HDU 1002大数求和

来源:互联网 发布:java为什么垃圾 编辑:程序博客网 时间:2024/06/11 05:45

题解

大数求和

代码

#include<iostream>  #include<cstring>  using namespace std;  void sum(int lar,int sma,char lng[],char sht[],char add[])  {      int p,q=0,i,j;      add[lar+1]='\0';      for(j=sma-1,i=lar-1;j>=0;j--,i--)          {              p=lng[i]-'0'+sht[j]-'0'+q;              if (p<10) {add[i+1]=p+'0';q=0;}              else {p=p%10; add[i+1]=p+'0';q=1;}          }      for(;i>=0;i--)          {              p=lng[i]-'0'+q;              if (p<10) {add[i+1]=p+'0';q=0;}              else {p=p%10; add[i+1]=p+'0';q=1;}          }      if (q==1) add[0]=1+'0';      else       {          for(i=0;i<lar+1;i++)              add[i]=add[i+1];      }  }  int main()  {      int T,n=1,k;      cin>>T;      k=T;      while(T--)      {          char a[1001],b[1001],c[1002];          int A,B;          cin>>a>>b;          A=strlen(a);          B=strlen(b);          if (A>B) sum(A,B,a,b,c);          else sum(B,A,b,a,c);          if(n!=k)          {cout<<"Case "<<n<<":"<<endl;          cout<<a<<" + "<<b<<" = "<<c<<endl<<endl;}          else                  {cout<<"Case "<<n<<":"<<endl;          cout<<a<<" + "<<b<<" = "<<c<<endl;}          n++;      }      return 0;  }  

题目

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

0 0
原创粉丝点击