hdu1002——A + B Problem II
2016-01-07 19:32
435 查看
A + B Problem II
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 288910 Accepted Submission(s): 55523
[/b]
[align=left]Problem Description[/align]
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 2
112233445566778899 998877665544332211
[align=left]Sample Output[/align]
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
hdu高精度加法练手题。。。
#include<iostream> #include<string.h> #include<stdlib.h> #include<stdio.h> using namespace std; void add(char a[],char b[],char back[]) { char *c; int i,j,k,up,z,x,y,l; if(strlen(a)>strlen(b)) l=strlen(a)+2;else l=strlen(b)+2; c=(char*)malloc(l*sizeof(char)); i=strlen(a)-1; j=strlen(b)-1; up=0;k=0; while(i>=0||j>=0) { if(i<0) x='0';else x=a[i]; if(j<0) y='0';else y=b[j]; z=x-'0'+y-'0'; if(up) z++; if(z>9) {z%=10;up=1;} else up=0; c[k++]=z+'0'; i--;j--; } if(up) c[k++]='1'; c[k]='0'; i=0; for(k-=1;k>=0;k--) back[i++]=c[k]; back[i]='\0'; } int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { char a[1000],b[1000],c[1000]; scanf("%s%s",a,b); add(a,b,c); printf("Case %d:\n",i); printf("%s + %s = %s\n",a,b,c); if(i<n) printf("\n"); } return 0; }
相关文章推荐
- onActivityResult与activity启动模式singleTask或singleInstance冲突
- 请你踏实一点!你没有你想的那么好
- Storage SPC标准测试
- 控制台界面控制(四)
- iOS编程 -- webView的使用
- Gmap.net 怎么导入离线地图
- Android安全-对称加密和非对称加密
- Oracle 11g 大小写问题
- zhi pian man tian fei
- Spring学习笔记—装配Bean
- Spring学习笔记—装配Bean
- Oracle 11g 密码过期解决方案
- explicit C++关键字
- 控制台界面控制(三):设置窗口标题
- kerberos认证协议
- startActivityForResult详解
- OK6410 Qt Segment fault
- 编辑距离问题求解思路
- Eclipse背景颜色修改
- Lucky 2048 - The secret of being lucky