10994 - Simple Addition(规律)
2013-11-18 21:19
309 查看
Problem E
Simple Addition
Input: Standard Input
Output: Standard Output
Let’s define a simple recursive function F (n), where
Let’s define another function S (p, q),
In this problem you have to Calculate S (p, q) on given value of p and q.
Input
The input file contains several lines of inputs. Each line contains two non negative integers p and q (p <= q) separated by a single space. p and q will fit in 32 bit signed integer. In
put is terminated by a line which contains two negative integers. This line should not be processed.
For each set of input print a single line of the value of S(p, q).
Sample Input Output for Sample Input
题意:求题目中那个公式从p到q F【i] 的和
思路:每个数肯定是1-9.这样的规律,个位1-9,十位1-9,百位1-9.如此一来,只要把各位加完,加十位,加百位,一个个加完,最后就是答案了。加的时候可以直接用等差数列和公式
代码:
#include <stdio.h>
int p, q;
long long Sum(long long s, long long e) {
if (s > e)
return 0;
return (s + e) * (e - s + 1) / 2;
}
long long cal(long long n) {
if (n <= 0) return 0;
long long ans = 0;
while (n) {
ans += n / 10 * 45;
ans += Sum(1, n % 10);
n /= 10;
}
return ans;
}
int main() {
while (~scanf("%d%d", &p, &q) && p != -1 && q != -1) {
printf("%lld\n", cal(q) - cal(p - 1));
}
return 0;
}
Simple Addition
Input: Standard Input
Output: Standard Output
Let’s define a simple recursive function F (n), where
Let’s define another function S (p, q),
In this problem you have to Calculate S (p, q) on given value of p and q.
Input
The input file contains several lines of inputs. Each line contains two non negative integers p and q (p <= q) separated by a single space. p and q will fit in 32 bit signed integer. In
put is terminated by a line which contains two negative integers. This line should not be processed.
For each set of input print a single line of the value of S(p, q).
Sample Input Output for Sample Input
1 10 10 20 30 40 -1 -1 | 46 48 52 |
思路:每个数肯定是1-9.这样的规律,个位1-9,十位1-9,百位1-9.如此一来,只要把各位加完,加十位,加百位,一个个加完,最后就是答案了。加的时候可以直接用等差数列和公式
代码:
#include <stdio.h>
int p, q;
long long Sum(long long s, long long e) {
if (s > e)
return 0;
return (s + e) * (e - s + 1) / 2;
}
long long cal(long long n) {
if (n <= 0) return 0;
long long ans = 0;
while (n) {
ans += n / 10 * 45;
ans += Sum(1, n % 10);
n /= 10;
}
return ans;
}
int main() {
while (~scanf("%d%d", &p, &q) && p != -1 && q != -1) {
printf("%lld\n", cal(q) - cal(p - 1));
}
return 0;
}
相关文章推荐
- uva 10994 - Simple Addition(规律)
- 10994 - Simple Addition(规律)
- uva 10994 - Simple Addition(规律)
- uva10994 - Simple Addition
- UVA 10994 Simple Addition
- UVA 10994 Simple Addition
- UVa 10994 - Simple Addition
- uva 10994 - Simple Addition
- UVa 10994 - Simple Addition
- UVA 10994 Simple Addition
- uva 10994 Simple Addition
- uva 10994 - Simple Addition
- UVA - 10994 Simple Addition
- UVa OJ 10994-Simple Addition
- Android View touch事件传递方式规律
- hdu 4925 贪心 自己从小到大做数据找方法规律
- uC/OSII 中各种事件管理方式的一般规律
- python赋值对象的指向改变规律
- UVA12627 Erratic Expansion (递归找规律)
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)(A)(模拟||规律)