A + B Problem (大数相加3种方法) (转自孙悦学长)
发布时间:2020-12-14 03:34:32 所属栏目:大数据 来源:网络整理
导读: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
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
?
Sample Output
解题思路:把两个数的每一位都存放到一个数组中,逆序存放,然后对应位相加的和存放到另一个数组中。 方法1:
#include <iostream> #include <string.h> using namespace std; int main() { char x[1001],y[1001];//用来存放输入的两个数,第一个大数存在x里面,第二个大数存在y里面 int xx[1001]={0},j=1;//xx数组(整型)用来存放x数组中数字的逆序,因为做加法的时候要从原数的末位开始相加,sum数组用来存放两个大数每位对应相加的值 int lenx,t; cin>>t; while(t--) { cin>>x>>y; lenx=strlen(x);//x的长度 leny=strlen(y);//y的长度 maxlen=lenx>leny?lenx:leny;//把最大长度赋给maxlen for(i=0;i<lenx;i++) xx[i]=x[lenx-i-1]-'0';//根据ascii码表 '0'为48例如 '4'为52 ,相减得到4 ,存放到整型数组xx中,注意x[lenx-i-1],意思是说逆序存放,为下面逆序相加做准备 for(i=0;i<leny;i++) yy[i]=y[leny-i-1]-'0'; for(i=0;i<maxlen;i++) { sum[i]+=xx[i]+yy[i];//对应位相加 sum[i+1]=sum[i]/10;//因为两个一位数相加不会超过18,如果sum[i]大于10,则sum[i+1]为1,也就是向高位进1 sum[i]=sum[i]%10;//如果对应为相加大于10,取余,相当于 -10; } if(sum[i]==1) maxlen++;//要注意i的值,它代表最高位的后一位,如果最高位大于10大话,要进1,也就是判断s[i]是否为一,进一意味着让位数maxlen+1 cout<<"Case "<<j<<":"<<endl; j++; cout<<x<<" + "<<y<<" = "; for(i=maxlen-1;i>=0;i--) cout<<sum[i]; cout<<endl; if(t!=0) cout<<endl;//没有这一句,AC不了,格式问题 for(i=0;i<maxlen;i++) { sum[i]=0; xx[i]=0; yy[i]=0; }//重置,为下一次输入做准备 } } 方法2:
#include <iostream> #include <string.h> using namespace std; int main() { char x[1001],y[10001]; int xx[1001]={0},maxlen; int t; cin>>t; while(t--) { cin>>x>>y; lenx=strlen(x); leny=strlen(y); maxlen=lenx>leny?lenx:leny; for(i=0;i<lenx;i++) xx[i]=x[lenx-i-1]-'0'; for(i=0;i<leny;i++) yy[i]=y[leny-i-1]-'0'; for(i=0;i<maxlen;i++) { sum[i]+=xx[i]+yy[i]; sum[i+1]=sum[i]/10; sum[i]=sum[i]%10;//算法和方法1相同 } if(sum[i]==1) maxlen++; cout<<"Case "<<j<<":"<<endl; j++; cout<<x<<" + "<<y<<" = "; for(i=maxlen-1;i>=0;i--) cout<<sum[i]; cout<<endl; if(t!=0) cout<<endl; memset(xx,1001*sizeof(int));//,1001);错误,整型四个字节,需要1001*4,即1001*sizeof(int) memset(yy,1001*sizeof(int)); memset(sum,1001*sizeof(int)); } } 方法3:
#include <iostream> #include <string.h> using namespace std; int main() { char x[1001],maxlen; int t; cin>>t; while(t--) { cin>>x>>y; lenx=strlen(x); leny=strlen(y); maxlen=lenx>leny?lenx:leny; for(i=0;i<lenx;i++) xx[i]=x[lenx-i-1]-'0'; for(i=0;i<leny;i++) yy[i]=y[leny-i-1]-'0'; for(i=0;i<maxlen;i++) { sum[i]+=xx[i]+yy[i]; if(sum[i]>=10)//判断两个大数的对应位相加是否大于等于10 { sum[i+1]=1;//如果大于10,高位进1 sum[i]-=10;//如果大于20,本位-10 } } if(sum[i]==1) maxlen++; cout<<"Case "<<j<<":"<<endl; j++; cout<<x<<" + "<<y<<" = "; for(i=maxlen-1;i>=0;i--) cout<<sum[i]; cout<<endl; if(t!=0) cout<<endl; memset(xx,1001*sizeof(int)); memset(yy,1001*sizeof(int)); } } 运行截图: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |