1002---A + B Problem II

http://acm.hdu.edu.cn/showproblem.php?pid=1002

 

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
 

 

Author
Ignatius.L
 

 

Recommend
We have carefully selected several similar problems for you:  1004 1003 1008 1005 1089 
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5  int a[1002]={0},b[1002]={0},c[1002]={0};
 6  int i,n,k,alen,blen,r=0;
 7  char str1[1002],str2[1002];
 8  scanf("%d",&n);
 9  while(n--)
10  {
11  if(r>0) printf("\n"); 
12   scanf("%s",str1); alen=strlen(str1);
13   scanf("%s",str2); blen=strlen(str2);
14   k=alen>blen?alen:blen;
15   for(i=0;i<alen;i++) a[i]=str1[alen-i-1]-'0';
16   for(i=0;i<blen;i++) b[i]=str2[blen-i-1]-'0';  
17   for(i=0;i<k;i++)
18   {
19    c[i]=a[i]+b[i]+c[i];
20    c[i+1]=c[i]/10;
21    c[i]=c[i]%10;
22   }
23   if(c[i]) k++;
24   r++;
25   printf("Case %d:\n",r);
26   printf("%s + %s = ",str1,str2);
27   for(i=k-1;i>=0;i--) printf("%d",c[i]);
28    printf("\n");
29   for(i=0;i<alen;i++) a[i]=0;
30   for(i=0;i<blen;i++) b[i]=0;
31   for(i=0;i<k;i++) c[i]=0;
32  }
33  return 0;
34 }
View Code

 

posted @ 2016-07-24 09:16  W-ning  阅读(152)  评论(0)    收藏  举报