HDU A + B Problem II 1002

A + B Problem II

http://acm.hdu.edu.cn/showproblem.php?pid=1002

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

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
 
2
1 2
112233445566778899 998877665544332211
 
Sample Output
 
Case 1:
1 + 2 = 3
 
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
 
Author
Ignatius.L
 
 
#include<iostream>
#include<cstring>

using namespace std;

int s1[1005],s2[1005],ans[1005];

int main(){
    int t;
    char str1[1005],str2[1005];
    cin>>t;
    int cases=0;
    while(t--){
        cin>>str1>>str2;
        if(cases)
            cout<<endl;
        int i,j;
        int len1=strlen(str1),len2=strlen(str2);
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        memset(ans,0,sizeof(ans));
        int si=0,sj=0;
        for(i=len1-1;i>=0;i--)
            s1[si++]=str1[i]-'0';
        for(j=len2-1;j>=0;j--)
            s2[sj++]=str2[j]-'0';

        for(i=0;i<1005;i++){
            ans[i]+=s1[i]+s2[i];
            if(ans[i]>=10){
                ans[i+1]+=ans[i]/10;
                ans[i]%=10;
            }
        }
        for(j=1004;j>=0;j--)
            if(ans[j]!=0)
                break;
        cout<<"Case "<<++cases<<":"<<endl;
        if(j==-1){
            cout<<"0 + 0 = 0"<<endl;
            continue;
        }
        cout<<str1<<" + "<<str2<<" = ";
        for(i=j;i>=0;i--)
            cout<<ans[i];
        cout<<endl;
    }
    return 0;
}

 

posted @ 2012-12-26 16:37  Jack Ge  阅读(438)  评论(0编辑  收藏  举报