标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 305856 Accepted Submission(s): 59088
#include<string> #include<iostream> #include<stdio.h> using namespace std; int main() { int n; string a,b; while(~scanf("%d",&n)) { int cas=1; for(int i=0; i<n; i++) { cin>>a>>b; int ans[10005]; for(int i=0; i<1005; i++) ans[i]=0; int tmp=0; int len=(a.length()<b.length())?a.length():b.length(); int llen=(a.length()>b.length())?a.length():b.length(); int t=0; /* for(int i=llen-1, j=len-1; j>-1 ; i--,j--) { if(a[i]-‘0‘+b[j]-‘0‘+tmp>9) { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp-10; tmp=1; } else { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp; tmp=0; } }*/ if(a.length()>=b.length()) { for(int i=llen-1, j=len-1; j>-1 ; i--,j--) { if(a[i]-‘0‘+b[j]-‘0‘+tmp>9) { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp-10; tmp=1; } else { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp; tmp=0; } } for(int i=llen-len-1; i>-1; i--) { if(tmp+a[i]-‘0‘>9) { ans[t++]=tmp-10+a[i]-‘0‘; tmp=1; } else { ans[t++]=a[i]+tmp-‘0‘; tmp=0; } } } if(a.length()<b.length()) { for(int j=llen-1, i=len-1; i>-1 ; i--,j--) { if(a[i]-‘0‘+b[j]-‘0‘+tmp>9) { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp-10; tmp=1; } else { ans[t++]=a[i]-‘0‘+b[j]-‘0‘+tmp; tmp=0; } } for(int i=llen-len-1; i>-1; i--) { if(tmp+b[i]-‘0‘>9) { ans[t++]=tmp-10+b[i]-‘0‘; tmp=1; } else { ans[t++]=b[i]+tmp-‘0‘; tmp=0; } } } if(tmp!=0) ans[t++]=tmp; cout<<"Case "<<cas++<<":"<<endl<<a<<" + "<<b<<" = "; for(int i=t-1; i>-1; i--) cout<<ans[i]; if(i!=n-1) printf("\n\n"); else printf("\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5444836.html