1002 A + B Problem II
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 o
utput 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.
utput 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
1 #include<iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <stdio.h> 5 using namespace std; 6 7 int main() 8 { 9 int n,c[1005],k,l,sum; 10 char a[1005],b[1005],e[2005],f[2005]; 11 while(cin>>n) 12 { 13 for(int j=1;j<=n;j++) 14 { 15 memset(e,'0',sizeof(e)); 16 memset(f,'0',sizeof(f)); 17 memset(c,0,sizeof(c)); 18 cin>>a>>b; 19 k=strlen(a);l=strlen(b); 20 strcpy(e+1000,a); 21 strcpy(f+1000,b); 22 for(int i=0;i<max(k,l);i++) 23 { 24 sum=(e[k-i+999]-'0')+(f[l-i+999]-'0')+c[i]; 25 c[i]=sum%10; 26 c[i+1]=sum/10; 27 } 28 printf("Case %d:\n",j); 29 printf("%s + %s = ",a,b); 30 if(c[max(k,l)]!=0) 31 cout<<c[max(k,l)]; 32 for(int i=max(k,l)-1;i>=0;i--) 33 cout<<c[i]; 34 cout<<endl; 35 if(j!=n) 36 cout<<endl; 37 } 38 39 } 40 return 0; 41 }
当前面的数对后面有影响时可以,转换变量来消除这种影响。