Integer Inquiry
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.)
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
#include <stdio.h> #include <string.h> char a1[1050],b1[1050]; int Judge(char ch[])//判断字符串ch是否全为0 { int i,k; k=strlen(ch); for(i=0;i<k;i++) { if(ch[i]!='0') return 0; } return 1; } char* bignumadd(char a1[],char a2[])//大数相加模版 { int i,j,k,lena,lenb; char c[1050]; int a[1050],b[1050],d[1050]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); lena=strlen(a1); lenb=strlen(b1);
//将加数和被加数转化为整型数组 for(i=0;i<lena;i++) a[i]=a1[lena-i-1]-'0'; for(i=0;i<lenb;i++) b[i]=b1[lenb-i-1]-'0'; k=lena>lenb?lena:lenb;//k为数组a1和b1的最大数位 for(i=0;i<k;i++) { d[i]=a[i]+b[i]+d[i]; d[i+1]+=d[i]/10;//向高位的进位 d[i]=d[i]%10; } while(d[k]) k++;//最后一位向高位进位,长度加1 while(!d[k-1]) k--;//消除前置0 for(j=0;j<k;j++) { c[j]=d[k-j-1]+'0';//将整型数组重新转化为字符数组 } if(Judge(c))//判断数组c是否全为0 strcpy(c,"0"); return c; } int main() { char c; int i=0; memset(a1,-1,sizeof(a1)); memset(b1,-1,sizeof(b1)); int flag=0; char *c1; while((scanf("%s",&a1))&&a1[0]!='0') { if(!flag) scanf("%s",&b1); c1=bignumadd(a1,b1); strcpy(b1,c1); flag=1; } printf("%s\n",b1); return 0; }