代码:
#include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; char s1[10000],s2[10000],s3[10000]; int main() { int t; scanf("%d",&t); int tt=0; while(t--) { ++tt; if(tt!=1) printf(" "); scanf("%s%s",s1,s2); int len1,len2,len3; len1=strlen(s1); len2=strlen(s2); len3=Min(len1,len2); int k=0; int i=len1-1; int j=len2-1; int pos=0; while(len3--) { int temp=(s1[i--]-'0'+s2[j--]-'0'+k); s3[pos++]=(temp%10+'0'); k=temp/10; } if(i==-1&&j==-1) { if(k!=0) s3[pos++]=k+'0'; } else if(i==-1&&j!=-1) { while(j>=0) { int temp=s2[j--]-'0'+k; s3[pos++]=(temp%10+'0'); k=temp/10; } if(k!=0) s3[pos++]=k+'0'; } else { while(i>=0) { int temp=s1[i--]-'0'+k; s3[pos++]=(temp%10+'0'); k=temp/10; } if(k!=0) s3[pos++]=k+'0'; } printf("Case %d: ",tt); printf("%s + %s = ",s1,s2); for(i=pos-1;i>=0;i--) { printf("%c",s3[i]); } printf(" "); } return 0; }