2012-03-23 21:30:22Morris

[UVA][Math] 10994 - Simple Addition

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

1 10

10 20

30 40

-1 -1

 

46

48

52




12345
有 1234 個 最後不為 0 的區間[0-10], * 45 ...

#include <stdio.h>
long long ZeroToN(long long n) {
    if(n <= 0)
        return 0;
    long long ans = 0, i;
    while(n != 0) {
        ans += 45*(n/10);
        for(i = n/10*10+1; i <= n; i++)
            ans += i%10;
        n /= 10;
    }
    return ans;
}
int main() {
    long long p, q;
    while(scanf("%lld %lld", &p, &q) == 2) {
        if(p < 0 && q < 0)
            break;
        printf("%lld\n", ZeroToN(q)-ZeroToN(p-1));
    }
    return 0;
}