[关闭]
@xunuo 2016-11-19T16:23:32.000000Z 字数 2394 阅读 2121

A + B Problem (Big integer version) 【高精度】

高精度

Time Limit: 1000 MS Memory Limit: 65536 KB

链接:https://www.oj.swust.edu.cn/problem/show/1202


Description

Input and output are the same with problem 1001.
 But A and B are big non-negative integers.
The biggest integer is less than 10^500.

Input

Input contains multiple test cases. 
Each case have two big non-negative integers a,b.

Output

For each case,Output a+b.

Sample Input

1234567890987654321
9876543210123456789
123456789123456789
321654987321654987
9081321110693270343633073697474256143563558458718976746753830538032062222085722974121768604305613921745580037409259811952655310075487163797179490457039169594160088430571674960498834085812920457916453747019461644031395307920624947349951053530086146486307198155590763466429392673709525428510973272600608981219760099374675982933766845473509473676470788342281338779191792495900393751209539300628363443012
6538005862664913074813656220643842443844131905754565672075358391135537108795991638155474452610874309742867231360502542308382199053675592825240788613991898567277116881793749340807728335795394301261629479870548736450984003401594705923178314906195914825136973281314862289454100745237769034410057080703111299605127114594552921209928891515242515620324828055912854227507525717981351447473570262981491527798

Sample Output

11111111101111111110
445111776445111776
15619326973358183418446729918118098587407690364473542418829188929167599330881714612277243056916488231488447268769762354261037509129162756622420279071031068161437205312365424301306562421608314759178083226890010380482379311322219653273129368436282061311444171436905625755883493418947294462921030353303720280824887213969228904143695736988751989296795616398194193006699318213881745198683109563609854970810

Hint

Note that there are leading zeros!

完整代码:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[505],b[505],sum[505];
  7. char s1[505],s2[505];
  8. int main()
  9. {
  10. while(scanf("%s%s",s1,s2)!=EOF)
  11. {
  12. memset(a,0,sizeof(a));
  13. memset(b,0,sizeof(b));
  14. memset(sum,0,sizeof(sum));
  15. int l1=strlen(s1);
  16. int l2=strlen(s2);
  17. for(int i=0;i<l1;i++)
  18. a[i]=s1[l1-1-i]-'0';//将s1里的数倒序存入a数组中
  19. for(int i=0;i<l2;i++)
  20. b[i]=s2[l2-1-i]-'0';//将s2里的数倒序存入a数组中
  21. int l=(l1>l2)?l1:l2;
  22. for(int i=0;i<l;i++)
  23. {
  24. sum[i]+=(a[i]+b[i]);//求和
  25. if(sum[i]>=10)//如果有进位
  26. {
  27. sum[i]-=10;//该位数减10
  28. sum[i+1]+=1;//下一位加1
  29. }
  30. }
  31. if(sum[l]>0)//如果最后的有进位,则再加一个长度
  32. l++;
  33. for(int i=l;i>0;i--)//去前导零
  34. {
  35. if(sum[i]==0)
  36. l--;
  37. else
  38. break;
  39. }
  40. for(int i=l;i>=0;i--)//输出该数
  41. printf("%d",sum[i]);
  42. printf("\n");
  43. }
  44. return 0;
  45. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注