hdu1002——A + B Problem II

来源:互联网 发布:暴走大事件淘宝店 编辑:程序博客网 时间:2024/05/22 10:32

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 288910    Accepted Submission(s): 55523


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
 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;}

0 0