Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
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.
Output
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. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3
Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
My solution:
#include<iostream> #include<string> using namespace std; int main(void) { int n,i,j; char a[1001],b[1001]; cin>>n; void plus(char *a,char *b); for (i=1;i<=n;i++) { cin>>a>>b; cout<<"Case "<<i<<":"<<endl<<a<<" + "<<b<<" = "; plus(a,b); cout<<endl; if (i!=n) cout<<endl; } return 0; } void plus(char *a,char *b) { int lena,lenb,k=0,t; lena=strlen(a); lenb=strlen(b); int i,j,na[1001],nb[1001],sum[1001]={0}; for (i=0;i<lena;i++) na[i]=a[i]-48; for (j=0;j<lenb;j++) nb[j]=b[j]-48; while(i>=0&&j>=0) { sum[k]=na[i]+nb[j]; j--; i--; k++; } if (j>=0) while (j>=0) { sum[k]=nb[j]; j--; k++; } else if (i>=0) while(i>=0) { sum[k]=na[i]; i--; k++; } for (t=1;t<k;t++) if (sum[t]>=10) { sum[t]%=10; sum[t+1]++; } while(sum[k]==0) k--; for (t=k;t>0;t--) cout<<sum[t]; }