A+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
*/

#include<stdio.h>
#include<string.h>
  int main(){
      int n;
      int len1,len2,i,j;
      int t; 
        int k=0;
       scanf("%d",&n);
       while(n--){
           char a1[1100],a2[1100];// 开始时,数组定义过小,导致超时 
           int a3[1100],a4[1100];
            int b2[1100];
            memset(a3,0,sizeof(a3));
            memset(a4,0,sizeof(a4));
             memset(b2,0,sizeof(b2));
            scanf("%s%s",a1,a2);
             k++;
        printf("Case %d:
",k);
        printf("%s",a1);
        printf(" + ");
        printf("%s",a2);
        printf(" = ");
           len1=strlen(a1);
           len2=strlen(a2);     //将两个字符数倒序,底位在前,高位在后       
        for(i=0;i<len1;i++){
          a3[i]=a1[i]-'0';
        }
          for(j=0;j<len2;j++){
              a4[j]=a2[j]-'0';
          }
         for(i=0;i<(len1/2);i++)
             {
          t=a3[i];
               a3[i]=a3[len1-i-1];
                   a3[len1-i-1]=t;    }
         
         for(j=0;j<(len2/2);j++){
              t=a4[j];
               a4[j]=a4[len2-j-1];
                 a4[len2-j-1]=t;
         }
         
           if(len1<len2)
             len1=len2;
             
           
       for(i=0;i<=len1;i++)  // 进位,相加大于9时,向下一位进1,并且将本位去十 
       { 
           b2[i]=(a3[i])+(a4[i]);
             if(b2[i]>9){
            
            a3[i+1]++; 
          b2[i]-=10;
                    }  }
        
        int i=len1+2;
        while(b2[i]==0)i--;//将高位的零去掉,并且倒序一次输出 
        for(;i>0;i--){
            printf("%d",b2[i]);
                  }
             
           printf("%d
",b2[0]);
           
            if(n)   printf("
");
       } 
       
  return 0;
}
原文地址:https://www.cnblogs.com/acmgym/p/3691864.html