您的位置:首页 > 其它

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   and q.

 

Input

The input file contains several lines of inputs. Each line contains two non negative integers 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

 

题意:求题目中那个公式从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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: