Integer Inquiry【大数的加法举例】
Integer Inquiry
Description 题目链接:http://poj.org/problem?id=1503 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 题目描述:输入多个大数,求其和,输入以0结束。
代码:
1 #include<iostream> 2 #include<string.h> 3 #include<stdlib.h> 4 using namespace std; 5 int main() 6 { 7 char f1[110]; 8 int sum[110]={0},i,j; 9 cin>>f1; 10 while(strcmp(f1,"0")!=0) 11 { 12 int t=strlen(f1),f[110]={0}; 13 for(i=t-1,j=0;i>=0;i--,j++) 14 f[j]=f1[i]-'0'; 15 int up=0; 16 for(j=0;j<=105;j++) 17 { 18 int s=f[j]+sum[j]+up; 19 sum[j]=s%10; 20 up=s/10; 21 } 22 cin>>f1; 23 } 24 for(i=105;i>=0;i--) 25 if(sum[i]!=0) 26 { 27 while(i>=0) 28 { 29 cout<<sum[i]; 30 i--; 31 } 32 break; 33 } 34 cout<<endl; 35 return 0; 36 } 37 38 39
|