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
- HDU 1002大数求和
- 大数求和(hdu 1047)
- 1002,大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- 大数求和
- HDU 1002 --大数问题
- hdu 1002 大数相加
- HDU 1002 大数
- hdu 1002(大数加法)
- hdu 1002 大数问题
- Apache web 服务器的下载和安装
- Python之快速排序算法实现(二)
- 【百金轻】:不要把“悲伤”留给油价
- Android layer-list的使用
- Unity3D游戏开发软件的基本组成有哪些?
- HDU 1002大数求和
- TTL电平和CMOS电平总结
- 业务表构建中一些特殊字符作为列名的构建示例
- Android - SurfaceView + MediaPlayer实现分段视频无缝播放
- (1.4) Trail ~ New Data Model
- 设置了auto-play 的ion-slide-box中切换tab时,内置计时器失效导致不自动播放的解决办法
- centos下安装jdk环境
- 【小松教你手游开发】【unity实用技能】控制renderQueue解决NGUI与Unity3D物体渲染顺序问题
- Mac下配置Android NDK