您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: