2 1 2 112233445566778899 998877665544332211
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110#include<stdio.h> #include<string.h> #define max(a,b) ((a>b)?a:b) int main() { char ta[5001],tb[5001]; int a[5001],b[5001],sum[5002],n,t,i,lena,lenb,lenmax; scanf("%d",&n); for (t=1;t<=n;t++) { scanf("%s %s",ta,tb); lena=strlen(ta); lenb=strlen(tb); lenmax=max(lena,lenb); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(sum,0,sizeof(sum)); //将a、b、sum三个数组初始化 for(i=0;i<=lena-1;i++) a[i]=ta[lena-1-i]-'0'; for(i=0;i<=lenb-1;i++) b[i]=tb[lenb-1-i]-'0'; //用ta、tb给a、b赋值 for(i=0;i<=lenmax-1;i++) { sum[i]+=(a[i]+b[i]); if(sum[i]>=10) { if(i==lenmax-1) lenmax++; sum[i]-=10; sum[i+1]++; } } //用for语句将a、b数组逐个相加,即相当于将两个数字从个位起逐位相加 printf("Case %d:\n",t); printf("%s + %s = ",ta,tb); for(i=lenmax-1;i>=0;i--) printf("%d",sum[i]); //反向输出sum数组 printf("\n");if (t<n) printf("\n"); } }