1002题大数的相加

来源:互联网 发布:淘宝手机12期分期付款 编辑:程序博客网 时间:2024/05/16 10:51

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

#include<stdio.h>#include<string.h> int main(){  int i,n;//定义一个需要输入循环加法的次数  scanf("%d",&n);//输入该次数  for(i=1;i<=n;i++)//开始循环  {    char s1[1001],s2[1001];//把数据存储在字符串数组中    int a[1001]={0},b[1001]={0},c[1001]={0};//int型数组     int len1,len2,len3;//保存字符串的长度    int max;//得到最大长度字符串    int q,k;//字符串转化为int型数组变量    int m,d;//数组相加的数组变量    int l;    scanf("%s%s",s1,s2);    len1=strlen(s1);    len2=strlen(s2);//输入字符串,获得字符串的长度    if(len1>len2)    {        max=len1;      }      else     {        max=len2;    }         //如果len1大于len2,最大长度字符串为len1         for(q=0,k=len1-1;q<len1;q++,k--)//字符串末尾数据放到了int数组的开头         {            a[q]=s1[k]-48;          }          for(q=0,k=len2-1;q<len2;q++,k--)//字符串末尾数据放到了int数组的开头         {            b[q]=s2[k]-48;          }          //把字符串型转化为int型         for(d=0,m=0;m<max;m++)         {            c[m]=(a[m]+b[m]+d)%10;            d=(a[m]+b[m]+d)/10;          }           printf("Case %d:\n%s + %s = ",i,s1,s2);          if(d!=0)          {           c[max]=1;          for(len3=max;len3>=0;len3--)          {            printf("%d",c[len3]);           }          }     else     {        for(len3=max-1;len3>=0;len3--)        {         printf("%d",c[len3]);         }     }     if(i!=n)     {        printf("\n\n");     }     else     {        printf("\n");     }   }    return 0;}
原创粉丝点击