NYOJ103A+B Problem II

A+B Problem II

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述

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
 1  
 2 /*function:   减法      */
 3 /* date:    2012  0725            */
 4 /*name:    赵杰迪             */
 5 /*environment:Dev-c++ 4.9.9.2 */
 6 #include<stdio.h>
 7 #include<string.h>
 8 #define maxn 1010
 9 int shu1[maxn],shu2[maxn];
10 char str1[maxn],str2[maxn];
11 int main()
12 {
13     int n,i,j,len1,len2,k;
14     scanf("%d",&n);
15     getchar();
16     for(k=1;k<=n;k++)
17     {
18         scanf("%s",str1);
19         scanf("%s",str2);
20         printf("Case %d:\n%s + %s = ",k,str1,str2);
21         memset(shu1,0,sizeof(shu1));
22         memset(shu2,0,sizeof(shu2));
23         len1=strlen(str1);
24         len2=strlen(str2);
25         //printf("lenstr1=%d,lenstr2=%d",len1,len2);//
26         for(j=0,i=len1-1;i>=0;i--)
27             shu1[j++]=str1[i]-'0';
28         for(j=0,i=len2-1;i>=0;i--)
29             shu2[j++]=str2[i]-'0';
30         for(i=0;i<maxn;i++)
31         {
32             shu1[i]+=shu2[i];
33             if(shu1[i]>=10)
34             {
35                 shu1[i]%=10;
36                 shu1[i+1]++;
37             }
38         }
39     //for(i=0;i<maxn;i++)
40         //printf("%d",shu1[i]);//
41        for(j=maxn-1;j>=0;j--)
42           if(shu1[j])break;
43        if(j==-1)
44            printf("0");
45        else 
46         {
47             for(i=j;i>=0;i--)
48            printf("%d",shu1[i]);
49         }
50      
51      printf("\n");
52     }
53 return 0;
54 }
55 
56 
57         
原文地址:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_2012_08_02.html