POJ 1503 Integer Inquiry

Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26570   Accepted: 10297

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

一道简单的高精度加法题~~

[C++]
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int sum[102]={0},temp[102],i;
10     char s[100];
11 
12     while(1)
13     {
14         int len,t,i,j;
15         scanf("%s",s);
16         len=strlen(s);
17         if(len==1&&s[0]=='0')
18             break;
19         //这里需要注意的是,把大整数从字符串转化到整型数组时,是要倒着转化过来的
20         for(i=0,j=len-1;j>=0;++i,--j)
21             temp[i]=s[j]-'0';
22         for(;i<102;++i)
23             temp[i]=0;
24         t=0;
25         for(i=0;i<102;i++)
26         {
27             int c=sum[i]+temp[i]+t;
28             sum[i]=c%10;
29             t=c/10;
30         }
31     }
32     for(i=101;sum[i]==0;--i);
33     for(;i>=0;--i)
34         cout<<sum[i];
35     cout<<endl;
36 
37     return 0;
38 }

 

posted @ 2013-05-02 19:26  ~~Snail~~  阅读(274)  评论(0编辑  收藏  举报