GDUFE ACM1002


A+B(Big Number Version)

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

    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 400.

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:

3
1 2
112233445566778899 998877665544332211
33333333333333333333333333 100000000000000000000

Sample Output:

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Case 3:
33333333333333333333333333 + 100000000000000000000 = 33333433333333333333333333
这道题难度中等
 1 核心代码:
 2        scanf("%s%s",s1,s2);
 3         len1=strlen(s1);   len2=strlen(s2);                                  
 4         for(i=len1-1,j=0;i>=0;i--)  //将s1字符串数组转换为数字数组,逆序
 5             num1[j++]=s1[i]-'0';
 6         for(i=len2-1,j=0;i>=0;i--)  
 7             num2[j++]=s2[i]-'0';   
 8         for(i=0;i<N;i++) 
 9         {  
10             num1[i]+=num2[i];   
11             if(num1[i]>9)      //进位
12             {   
13                 num1[i]-=10;       //加法最大是9+9=18
14                 num1[i+1]++;     //所以加1
15             } 
16         }           
17         printf("Case %d:\n%s + %s = ",X,s1,s2);
18         X++;
19         for(i=N-1;(i>=0)&&(num1[i]==0);i--);     //空语句去掉多余的0
20         for(;i>=0;i--)    
21             printf(“%d”,num1[i]);   //逆序输出结果
22         

另附一种AC代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char a[1000],b[1000],c[1001];
 6     int i,j=1,p=0,n,n1,n2;
 7     scanf("%d",&n);
 8     while(n)
 9     {
10         scanf("%s %s",a,b);
11         printf("Case %d:\n",j);
12         printf("%s + %s = ",a,b);
13         n1=strlen(a)-1;
14         n2=strlen(b)-1;
15         for(i=0;n1>=0||n2>=0;i++,n1--,n2--)
16         {
17             if(n1>=0&&n2>=0){c[i]=a[n1]+b[n2]-'0'+p;}
18             if(n1>=0&&n2<0){c[i]=a[n1]+p;}
19             if(n1<0&&n2>=0){c[i]=b[n2]+p;}
20             p=0;
21             if(c[i]>'9'){c[i]=c[i]-10;p=1;}
22         }
23         if(p==1){printf("%d",p);}
24         while(i--)
25         {printf("%c",c[i]);}
26         j++;
27         if(n!=1){printf("\n\n");}
28         else {printf("\n");}
29         n--;
30     }
31 }
原文地址:https://www.cnblogs.com/2119662736lzj/p/6012642.html