POJ 1503 Integer Inquiry

Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23072   Accepted: 8987

Description

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670

Source

 
 
只有正数的高精度加法。用字符串就好。
 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cstdio>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int result[105];
10 
11 void add(string line)
12 {
13     int current = 104;
14     for(int i(line.size()-1);i >= 0;--i)
15     {
16         result[current] += (line[i]-'0');
17         if(result[current] >= 10)
18         {
19             result[current-1]++;
20             result[current] -= 10;
21         }
22         current--;
23     }
24     if(result[current] >= 10)
25     {
26         result[current-1]++;
27         result[current] -= 10;
28     }
29 }
30 
31 int main(void)
32 {
33     string line;
34     memset(result,0,sizeof(int)*105);
35     
36     while(cin >> line && line != "0")
37     {
38         add(line);
39     }
40     
41     bool flag = false;
42     for(int i = 0;i !=105;++i)
43     {
44         if(result[i] == 0 && !flag)
45             continue;
46         else
47         {
48             flag = true;
49             printf("%d",result[i]);
50         }
51     }
52     printf("\n");
53     
54     return 0;
55 }

 

posted @ 2012-04-24 17:23  gluowei39  阅读(199)  评论(0编辑  收藏  举报