A+B Problem II
来源:互联网 发布:mac os x常用软件 编辑:程序博客网 时间:2024/05/23 21:52
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
A,B must be positive.
- 输入
- 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.
- 输出
- 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.
- 样例输入
21 2112233445566778899 998877665544332211
样例输出
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
//最开始写的较笨的方法
#include<stdio.h>#include<string.h>int main(){int n,x,y,i,j,d,p;char a[1000],b[1000],e[1001];scanf("%d",&n);for(d=1;d<=n;d++){scanf("%s %s",&a,&b);printf("Case %d:\n",d);x=strlen(a)-1;y=strlen(b)-1;p=0;for(i=0;x>=0||y>=0;x--,y--){if(x>=0&&y>=0)e[i]=a[x]+b[y]-'0'+p;if(x>=0&&y<0)e[i]=a[x]+p;if(x<0&&y>=0)e[i]=b[y]+p;p=0;if(e[i]>'9'){e[i]=e[i]-10;p=1;}i++;}printf("%s + %s = ",a,b);if(p==1)printf("1");for(j=i-1;j>=0;j--)printf("%d",e[j]-'0'); //printf("%c",e[j]);printf("\n");if(d!=n)printf("\n");}return 0;}
//之后的方法
<span style="font-size:18px;">#include<iostream>#include<string.h>using namespace std;int main(){char a[1000],c[1000];int i,j,x,n,d;cin>>n; for(d=1;d<=n;d++){int sum[1000]={0};cin>>a>>c;cout<<"Case "<<d<<":"<<endl;cout<<a<<" + "<<c<<" = ";int t1=strlen(a);int t2=strlen(c);x=999;for(i=t1-1,j=t2-1;i>=0&&j>=0;i--,j--)sum[x--]=(a[i]-'0')+(c[j]-'0');if(i>=0)sum[x--]=a[i--]-'0';if(j>=0)sum[x--]=c[j--]-'0';x=0;for(i=999;i>=0;i--){sum[i]+=x;x=0;if(sum[i]>9){x=sum[i]/10;sum[i]=sum[i]%10;}} for(i=0;i<1000;i++){if(sum[i]!=0)break;}for(j=i;j<1000;j++)cout<<sum[j];cout<<endl;if(d!=n)cout<<endl;}return 0;}</span>
0 0
- A + B Problem II
- A + B Problem II
- A + B problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A+B Problem II
- A+B problem II
- A*B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- A + B Problem II
- 悼念512汶川大地震遇难同胞——来生一起走
- 经典的0、1背包
- 笔记2_看张鹏老师html+css视频所得
- Eclipse中应用的调试
- 开发环境搭建中的网络代理问题
- A+B Problem II
- HDOJ1231DP
- 非模态对话框的创建及撤销
- SDUT 2498 AOE网上的关键路径(SPFA 最长路径)
- poj 1101 The Game bfs
- 利用指针传递二维数组进行行列式转置
- 算法导论2.4 合并排序求逆序数
- dsp EDMA学习与疑问
- [ACM] sdut 2877 angry_birds_again_and_again (简单数学积分)