描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
A,B must be positive.
-
输入
- The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000. 输出
- For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. 样例输入
-
2 1 2 112233445566778899 998877665544332211
样例输出
-
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
-
-
-
-
-
-
-
-
-
//最开始写的较笨的方法
-
#include<stdio.h> #include<string.h> int main() { int n,x,y,i,j,d,p; char a[1000],b[1000],e[1001]; scanf("%d",&n); for(d=1;d<=n;d++) { scanf("%s %s",&a,&b); printf("Case %d:\n",d); x=strlen(a)-1; y=strlen(b)-1; p=0; for(i=0;x>=0||y>=0;x--,y--) { if(x>=0&&y>=0) e[i]=a[x]+b[y]-'0'+p; if(x>=0&&y<0) e[i]=a[x]+p; if(x<0&&y>=0) e[i]=b[y]+p; p=0; if(e[i]>'9') { e[i]=e[i]-10; p=1; } i++; } printf("%s + %s = ",a,b); if(p==1) printf("1"); for(j=i-1;j>=0;j--) printf("%d",e[j]-'0'); //printf("%c",e[j]); printf("\n"); if(d!=n) printf("\n"); } return 0; }
-
//之后的方法
-
<span style="font-size:18px;">#include<iostream> #include<string.h> using namespace std; int main() { char a[1000],c[1000]; int i,j,x,n,d; cin>>n; for(d=1;d<=n;d++) { int sum[1000]={0}; cin>>a>>c; cout<<"Case "<<d<<":"<<endl; cout<<a<<" + "<<c<<" = "; int t1=strlen(a); int t2=strlen(c); x=999; for(i=t1-1,j=t2-1;i>=0&&j>=0;i--,j--) sum[x--]=(a[i]-'0')+(c[j]-'0'); if(i>=0) sum[x--]=a[i--]-'0'; if(j>=0) sum[x--]=c[j--]-'0'; x=0; for(i=999;i>=0;i--) { sum[i]+=x; x=0; if(sum[i]>9) { x=sum[i]/10; sum[i]=sum[i]%10; } } for(i=0;i<1000;i++) { if(sum[i]!=0) break; } for(j=i;j<1000;j++) cout<<sum[j]; cout<<endl; if(d!=n) cout<<endl; } return 0; }</span>
-