A + B Problem (大数相加3种方法) (转自孙悦学长)

来源:互联网 发布:顶级源码mx800 编辑:程序博客网 时间:2024/05/16 06:55
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
21 2112233445566778899 998877665544332211
 

Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110

解题思路:把两个数的每一位都存放到一个数组中,逆序存放,然后对应位相加的和存放到另一个数组中。

方法1:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char x[1001],y[1001];//用来存放输入的两个数,第一个大数存在x里面,第二个大数存在y里面  
  7.     int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1;//xx数组(整型)用来存放x数组中数字的逆序,因为做加法的时候要从原数的末位开始相加,sum数组用来存放两个大数每位对应相加的值  
  8.     int lenx,leny,maxlen,t;  
  9.     cin>>t;  
  10.     while(t--)  
  11.     {  
  12.         cin>>x>>y;  
  13.         lenx=strlen(x);//x的长度   
  14.         leny=strlen(y);//y的长度   
  15.         maxlen=lenx>leny?lenx:leny;//把最大长度赋给maxlen  
  16.         for(i=0;i<lenx;i++)  
  17.             xx[i]=x[lenx-i-1]-'0';//根据ascii码表 '0'为48例如 '4'为52 ,相减得到4 ,存放到整型数组xx中,注意x[lenx-i-1],意思是说逆序存放,为下面逆序相加做准备  
  18.         for(i=0;i<leny;i++)  
  19.             yy[i]=y[leny-i-1]-'0';  
  20.         for(i=0;i<maxlen;i++)  
  21.         {  
  22.             sum[i]+=xx[i]+yy[i];//对应位相加   
  23.             sum[i+1]=sum[i]/10;//因为两个一位数相加不会超过18,如果sum[i]大于10,则sum[i+1]为1,也就是向高位进1  
  24.             sum[i]=sum[i]%10;//如果对应为相加大于10,取余,相当于 -10;  
  25.         }  
  26.         if(sum[i]==1)  
  27.             maxlen++;//要注意i的值,它代表最高位的后一位,如果最高位大于10大话,要进1,也就是判断s[i]是否为一,进一意味着让位数maxlen+1  
  28.         cout<<"Case "<<j<<":"<<endl;  
  29.         j++;  
  30.         cout<<x<<" + "<<y<<" = ";  
  31.         for(i=maxlen-1;i>=0;i--)  
  32.             cout<<sum[i];  
  33.         cout<<endl;  
  34.         if(t!=0)  
  35.             cout<<endl;//没有这一句,AC不了,格式问题  
  36.         for(i=0;i<maxlen;i++)  
  37.         {  
  38.             sum[i]=0;  
  39.             xx[i]=0;  
  40.             yy[i]=0;  
  41.         }//重置,为下一次输入做准备   
  42.   
  43.     }  
  44.   
  45. }  



方法2:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char x[1001],y[10001];  
  7.     int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1,lenx,leny,maxlen;  
  8.     int t;  
  9.     cin>>t;  
  10.     while(t--)  
  11.        {  
  12.   
  13.         cin>>x>>y;  
  14.         lenx=strlen(x);  
  15.         leny=strlen(y);  
  16.         maxlen=lenx>leny?lenx:leny;  
  17.         for(i=0;i<lenx;i++)  
  18.             xx[i]=x[lenx-i-1]-'0';  
  19.         for(i=0;i<leny;i++)  
  20.             yy[i]=y[leny-i-1]-'0';  
  21.         for(i=0;i<maxlen;i++)  
  22.         {  
  23.             sum[i]+=xx[i]+yy[i];  
  24.             sum[i+1]=sum[i]/10;  
  25.             sum[i]=sum[i]%10;//算法和方法1相同  
  26.         }  
  27.         if(sum[i]==1)  
  28.             maxlen++;  
  29.         cout<<"Case "<<j<<":"<<endl;  
  30.         j++;  
  31.         cout<<x<<" + "<<y<<" = ";  
  32.         for(i=maxlen-1;i>=0;i--)  
  33.             cout<<sum[i];  
  34.         cout<<endl;  
  35.         if(t!=0)  
  36.             cout<<endl;  
  37.         memset(xx,0,1001*sizeof(int));//,把xx数组里的值清0,memset是按照字节进行赋值,一开始写成memset(xx,0,1001);错误,整型四个字节,需要1001*4,即1001*sizeof(int)  
  38.         memset(yy,0,1001*sizeof(int));  
  39.         memset(sum,0,1001*sizeof(int));  
  40.   
  41.     }  
  42.   
  43. }  

方法3:

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2. #include <string.h>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char x[1001],y[10001];  
  7.     int xx[1001]={0},yy[1001]={0},sum[1001]={0},i,j=1,lenx,leny,maxlen;  
  8.     int t;  
  9.     cin>>t;  
  10.     while(t--)  
  11.        {  
  12.   
  13.         cin>>x>>y;  
  14.         lenx=strlen(x);  
  15.         leny=strlen(y);  
  16.         maxlen=lenx>leny?lenx:leny;  
  17.         for(i=0;i<lenx;i++)  
  18.             xx[i]=x[lenx-i-1]-'0';  
  19.         for(i=0;i<leny;i++)  
  20.             yy[i]=y[leny-i-1]-'0';  
  21.         for(i=0;i<maxlen;i++)  
  22.         {  
  23.             sum[i]+=xx[i]+yy[i];  
  24.             if(sum[i]>=10)//判断两个大数的对应位相加是否大于等于10  
  25.             {  
  26.                 sum[i+1]=1;//如果大于10,高位进1   
  27.                 sum[i]-=10;//如果大于20,本位-10  
  28.             }  
  29.         }  
  30.         if(sum[i]==1)  
  31.             maxlen++;  
  32.         cout<<"Case "<<j<<":"<<endl;  
  33.         j++;  
  34.         cout<<x<<" + "<<y<<" = ";  
  35.         for(i=maxlen-1;i>=0;i--)  
  36.             cout<<sum[i];  
  37.         cout<<endl;  
  38.         if(t!=0)  
  39.             cout<<endl;  
  40.         memset(xx,0,1001*sizeof(int));  
  41.         memset(yy,0,1001*sizeof(int));  
  42.         memset(sum,0,1001*sizeof(int));  
  43.   
  44.     }  
  45.   
  46. }  


运行截图:



0 0
原创粉丝点击