hdu 1002
来源:互联网 发布:富盈网络水军 编辑:程序博客网 时间:2024/05/16 05:42
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 167864 Accepted Submission(s): 32130
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
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
Author
Ignatius.L
模拟大数相加
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int bigadd(char a[],char b[],int sum[]){ int lena,lenb,lens,i; char m[100004],n[100004]; memset(m,'\0',sizeof(m)); memset(n,'\0',sizeof(n)); lena=strlen(a); lenb=strlen(b); lens=(lena>lenb)?lena:lenb; //cout<<lens; int c=0; for(i=0;i<lena;i++) { m[i]=a[lena-i-1]-48; //printf("%d",m[i]); } m[lena]='\0'; //cout<<m; for(i=0;i<lenb;i++) { n[i]=b[lenb-i-1]-'0'; //printf("%c",n[i]); } n[lenb]='\0'; //cout<<n; for(i=0;i<lena||i<lenb;i++) { sum[i]=(m[i]+n[i]+c)%10; //cout<<sum[i]; c=(m[i]+n[i]+c)/10; sum[i+1]=c; } //cout<<sum<<endl; if(c) { lens++; } return lens;}int main(){ int c,i,j,k,t; char a[100004],b[100005]; int sum[100004]; scanf("%d",&c); for(t=1;t<=c;t++) { memset(a,'\0',sizeof(a)); memset(b,'\0',sizeof(b)); memset(sum,'\0',sizeof(sum)); scanf("%s",a); scanf("%s",b); k=bigadd(a,b,sum); if(t==c) { printf("Case %d:\n",t); cout<<a<<' '<<'+'<<' '<<b<<' '<<'='<<' '; for(i=k-1;i>=0;i--) printf("%d",sum[i]); cout<<endl; } else { printf("Case %d:\n",t); //cout<<a<<'+'<<b<<'='; cout<<a<<' '<<'+'<<' '<<b<<' '<<'='<<' '; for(i=k-1;i>=0;i--) printf("%d",sum[i]); cout<<endl<<endl; } } return 0;}
- HDU 1002
- hdu 1002
- HDU-1002
- hdu 1002
- HDU 1002
- HDU 1002
- hdu 1002
- hdu 1002
- hdu 1002
- hdu 1002
- HDU 1002
- hdu 1002
- hdu 1002
- hdu 1002
- HDU 1002
- HDU 1002
- hdu 1002
- hdu 1002
- XMPP聊天客户端环境搭建
- 有中国电信手机一定要看。CTWAP和CTNET是什么意思?有什么区别?
- UVa:357 Let Me Count The Ways
- Ubuntu右键菜单添加新建OpenOffice文件
- rild 源码分析
- hdu 1002
- .Net MVC4 使用心得(五)细节问题Url.Action和Html.ActionLink
- 简单介绍Java EE容器
- VC++之屏幕抓取
- 第一篇文章
- xml--通过jdom解析及生产XML
- J2EE初学者要理解的几个问题
- operator new和operator delete->优化内存分配
- ubuntu12.04 显示桌面快捷键,命令