A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 318065 Accepted Submission(s): 61825
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
字符串数组模拟
1 #include<iostream> 2 #include<cstring> 3 #include<string.h> 4 #include<cmath> 5 #include<algorithm> 6 typedef long long ll; 7 using namespace std; 8 #define N 1010 9 int main() 10 { 11 int n,k,i,j,flag; 12 char s1[N],s2[N]; 13 int a[N],b[N]; 14 int c[N]; 15 cin>>n; 16 for(k=1;k<=n;k++) 17 { 18 cin>>s1>>s2; 19 int len1=strlen(s1); 20 int len2=strlen(s2); 21 cout<<"Case "<<k<<":"<<endl<<s1<<" + "<<s2<<" = "; 22 memset(a,0,sizeof(a)); 23 memset(b,0,sizeof(b)); 24 int j=0; 25 int jj=0; 26 for(i=len1-1;i>=0;i--) 27 { 28 a[j]=s1[i]-'0'; 29 j++; 30 } 31 for(i=len2-1;i>=0;i--) 32 { 33 b[jj]=s2[i]-'0'; 34 jj++; 35 } 36 memset(c,0,sizeof(c)); 37 for(i=0;i<=max(len1-1,len2-1);i++) 38 { 39 c[i]=a[i]+b[i]+c[i]; 40 if(c[i]>9) 41 { 42 c[i]=c[i]-10; 43 c[i+1]++; 44 } 45 } 46 flag=0; 47 for(i=max(len1-1,len2-1);i>=0;i--) 48 { 49 if(flag==0&&c[i]==0) 50 flag=0; 51 else 52 { 53 cout<<c[i]; 54 flag=1; 55 } 56 } 57 if(k<n) 58 cout<<endl<<endl; 59 else 60 cout<<endl; 61 } 62 return 0; 63 }