Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 435932 Accepted Submission(s): 84825
代码:
1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4 char a[1005],b[1005]; 5 int c[1005],d[1005],ans[1005]; 6 int n,i,k,j,h,t,l; 7 int len1,len2; 8 scanf("%d",&n); 9 h=0; 10 while(n--){ 11 h+=1; 12 for(i=0;i<1005;i++){ 13 c[i]=d[i]=0; 14 ans[i]=0; 15 } 16 scanf("%s %s",&a,&b); 17 len1=strlen(a); 18 len2=strlen(b); 19 for(i=0;i<len1;i++){ 20 c[i]=a[i]-'0'; 21 } 22 for(i=0;i<len2;i++){ 23 d[i]=b[i]-'0'; 24 } 25 printf("Case %d:\n",h); 26 for(i=0;i<len1;i++) 27 printf("%d",c[i]); 28 printf(" + "); 29 for(i=0;i<len2;i++) 30 printf("%d",d[i]); 31 printf(" = "); 32 for(i=0,j=len1-1;i<len1/2;i++,j--){ 33 t=c[j]; 34 c[j]=c[i]; 35 c[i]=t; 36 } 37 for(i=0,j=len2-1;i<len2/2;i++,j--){ 38 t=d[j]; 39 d[j]=d[i]; 40 d[i]=t; 41 } 42 l=len1>len2?len1:len2; 43 for(i=0,k=0;i<=l;i++,k++){ 44 if(c[i]+d[i]<10) 45 ans[k]=c[i]+d[i]; 46 else{ 47 ans[k]=(c[i]+d[i])%10; 48 c[i+1]+=(c[i]+d[i])/10; 49 } 50 51 } 52 for(j=k-1;j>=0;j--){ 53 if(j==k-1&&ans[j]==0) 54 continue; 55 printf("%d",ans[j]); 56 } 57 if(n==0) 58 printf("\n"); 59 else 60 printf("\n\n"); 61 } 62 return 0; 63 }