1632 - Integer Inquiry
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.)
题目输入
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.
题目输出
Your program should output the sum of the VeryLongIntegers given in the input.
输入/输出样例
题目输入
02142365262 640923185904378532104 5824357832190450385904375987431254 0
题目输出
5824357832191091309090282508328620
提示
高精度加法
C语言解答
#include<stdio.h> #include<string.h> int main() { int i,j,c,up; char a[10000],b[10000]="0",temp; while(gets(a)) { if(strlen(a)==1&&a[0]=='0') { j=strlen(b)-1; for(i=0;i<strlen(b)/2;i++) { temp=b[i]; b[i]=b[j]; b[j]=temp; j--; } puts(b); break; } j=strlen(a)-1; for(i=0;i<strlen(a)/2;i++) { temp=a[i]; a[i]=a[j]; a[j]=temp; j--; } if(strlen(a)>strlen(b)) { for(i=strlen(b);i<strlen(a);i++) { b[i]='0'; } } else { for(i=strlen(a);i<strlen(b);i++) { a[i]='0'; } } for(i=0;i<strlen(a);i++) { c=(a[i]-'0'+b[i]-'0'); if(up==1) { c++; up=0; } if(c<10) b[i]=c+'0'; else { c=c-10; b[i]=c+'0'; up=1; } } if(up==1) { b[strlen(b)]='1'; } } return 0; }
C++解答
#include<iostream> #include <string.h> using namespace std; int main() { char a[105][105]; int b[105][105],sum[110]; memset(sum, 0, sizeof(sum)) ; //将sum[]数组清零 int i,len,m,k,g; for(i=0;i<105;i++) { cin.getline(a[i],105,'\n'); len=strlen(a[i]); if(len==1 && a[i][0]=='0') break; //i为输入大数的个数 } for(m=0;m<i;m++) { len=strlen(a[m]); g=0; for(k=len-1;k>=0;k--) { b[m][g]=a[m][k]-'0'; g++; //实现逆序存放 } } for(m=0;m<i;m++) { len=strlen(a[m]); for(k=0;k<len;k++) { sum[k]+=b[m][k]; //实现相加 } } for(k=0;k<104;k++) //处理结果中的进位问题 { if(sum[k] > 9) { sum[k + 1] += sum[k] / 10 ; sum[k] %= 10 ; } } for(k=109;k>=0;k--) { if(sum[k]!=0) break; } if(k==0) cout<<sum[0]; else { for(m=k;m>=0;m--) cout<<sum[m]; } cout<<endl; return 0; }
提示
高精度加法