加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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
  
  
2 1 2 112233445566778899 998877665544332211
?

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

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

方法1:

[cpp] view plain copy print ?
  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 plain copy print ?
  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},j=1,lenx,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,1001*sizeof(int));//,把xx数组里的值清0,memset是按照字节进行赋值,一开始写成memset(xx,1001);错误,整型四个字节,需要1001*4,即1001*sizeof(int) ??
  38. ????????memset(yy,1001*sizeof(int));??
  39. ????????memset(sum,1001*sizeof(int));??
  40. ??
  41. ????}??
  42. ??
  43. }??

方法3:

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


运行截图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读