24h購物| | PChome| 登入
2012-03-23 21:30:22| 人氣2,618| 回應0 | 上一篇 | 下一篇

[UVA][Math] 10994 - Simple Addition

推薦 0 收藏 0 轉貼0 訂閱站台

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;
}

台長: Morris
人氣(2,618) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Sort&Search] 10125 - Sumsets
此分類上一篇:[UVA] 10699 - Count the factors

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文