HDU1002

来源:互联网 发布:计算机通信网络实训 编辑:程序博客网 时间:2024/06/04 01:15

A + B Problem II

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


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


题目大意:高精度加法

代码:

#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;char a[20000],b[20000];int n,A[20000],B[20000],Ans[20000];int max(int a,int b){    return a>b?a:b;}int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++){        for(int j=1;j<=18888;j++){            Ans[j]=0; A[j]=0; B[j]=0;        }        printf("Case %d:\n",i);        scanf("%s%s",a,b);        printf("%s + %s = ",a,b);        if(a[0]=='0' && b[0]=='0' && strlen(a)==1 && strlen(b)==1){            printf("0\n");            if(i!=n)printf("\n");            continue;        }        for(int j=1;j<=strlen(a);j++)A[strlen(a)-j+1]=a[j-1]-48; A[0]=strlen(a);        for(int j=1;j<=strlen(b);j++)B[strlen(b)-j+1]=b[j-1]-48; B[0]=strlen(b);        for(int j=1;j<=max(A[0],B[0]);j++)Ans[j]=A[j]+B[j]; Ans[0]=max(A[0],B[0]);        for(int j=1;j<=Ans[0]-1;j++){            Ans[j+1]+=Ans[j]/10;            Ans[j]%=10;        }        while(Ans[Ans[0]]){            Ans[Ans[0]+1]+=Ans[Ans[0]]/10;            Ans[Ans[0]]%=10;            Ans[0]++;        }        for(int j=Ans[0]-1;j>=1;j--)printf("%d",Ans[j]);        printf("\n");        if(i!=n)printf("\n");    }    return 0;}




0 0
原创粉丝点击