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 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 <stdio.h> 
  2 #include <string.h>
  3 #define N 1001
  4 
  5 int main(){
  6     int T;
  7     char A[N];
  8     char B[N];
  9     int sum[N];
 10     char temp[N];
 11     int A_length;
 12     int B_length;
 13     int i;
 14     int length;   //长度差
 15     int first;
 16     int second;
 17     int flag;
 18     int flag2;
 19     int time;
 20 
 21     scanf("%d",&T);
 22     time=1;    //time表示第几个输入数据组
 23 
 24     while(T--){
 25         flag=0;
 26         flag2=0;
 27         scanf("%s%s",A,B);
 28 
 29         printf("Case %d:
",time);
 30         time++;
 31         printf("%s + %s = ",A,B);
 32 
 33         A_length=strlen(A);
 34         B_length=strlen(B);
 35 
 36         if(A_length<B_length){   //保证A的长度大于或者等于B的长度
 37             strcpy(temp,A);
 38             strcpy(A,B);
 39             strcpy(B,temp);
 40         }
 41         
 42         A_length=strlen(A);
 43         B_length=strlen(B);
 44         length=A_length-B_length;
 45         for(i=B_length-1;i>=0;i--){  //数组B往后移动
 46             B[i+length]=B[i];
 47         }
 48 
 49         for(i=0;i<length;i++)   //数组B前面添加'0'
 50             B[i]='0';
 51 
 52         B[A_length]='';  //数组B添加结束符号
 53 
 54         for(i=A_length-1;i>=0;i--){
 55             first=A[i]-'0';
 56             second=B[i]-'0';
 57             sum[i]=(first+second)%10;   
 58             
 59             if(i>0)
 60                 A[i-1]=A[i-1]-'0'+(first+second)/10+'0';
 61         }
 62 
 63         first=A[0]-'0';
 64         second=B[0]-'0';
 65 
 66         flag=(first+second)/10;
 67 
 68         if(flag)
 69             printf("%d",flag);
 70 
 71         for(i=0;i<A_length;i++){
 72             if(flag){
 73                 flag2=1;
 74                 printf("%d",sum[i]);
 75             }
 76 
 77             else{
 78                 if(sum[i]!=0){
 79                     flag2=1;
 80                     printf("%d",sum[i]);
 81                 }
 82 
 83                 else{
 84                     if(flag2==1)
 85                         printf("%d",sum[i]);
 86                 }
 87             }
 88 
 89         }
 90 
 91         if(flag2==0)
 92             printf("0");
 93 
 94         printf("
");
 95 
 96         if(T!=0)
 97             printf("
");
 98     }
 99         
100     return 0;
101 }
原文地址:https://www.cnblogs.com/zqxLonely/p/4063693.html