HDUoj(1002)A + B Problem II

A + B ProblemII

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

Problem Description

I have a verysimple problem for you. Given two integers A and B, your job is to calculatethe Sum of A + B. 

Input

The first line ofthe input contains an integer T(1<=T<=20) which means the number of testcases. Then T lines follow, each line consists of two positive integers, A andB. Notice that the integers are very large, that means you should not processthem by using 32-bit integer. You may assume the length of each integer willnot exceed 1000.

Output

For each testcase, 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 intthe equation. Output a blank line between two test cases.

Sample Input

2

1 2

112233445566778899998877665544332211

Sample Output

Case 1:

1 + 2 = 3

 

Case 2:

112233445566778899+ 998877665544332211 = 1111111111111111110

#include<iostream>
#include<string.h>
using namespace std;
#define MAXNUM 1000
int a1[MAXNUM + 5];
int a2[MAXNUM + 5];
char num1[MAXNUM + 5];
char num2[MAXNUM + 5];
int main()
{
    int N;
    int len1, len2; 
    int i, j, k, l,p;
    int ans = 0;
    cin >> N;
    for (p = 1; p <= N;p++)
    {
        
        cin >> num1 >> num2;
        len1 = strlen(num1);
        len2 = strlen(num2);
        memset(a1, 0, sizeof(a1));
        memset(a2, 0, sizeof(a2));
        j = 0;
        for (i = len1 - 1; i >= 0; i--)
        {
            a1[j] = num1[i] - '0';
            j++;
        }

        l = 0;
        for (k = len2 - 1; k >= 0; k--)
        {
            a2[l] = num2[k] - '0';
            l++;
        }

        int temp;
        if (len1 < len2)
        {
            temp =len1 ;
            len1 = len2;
            len2 = temp;
        }
        int t1, t2;
        for (t1 = 0; t1 < len1; t1++)
        {
            a1[t1] = a1[t1] + a2[t1];
            if (a1[t1] >= 10)
            {
                a1[t1] = a1[t1] - 10;
                a1[t1 + 1] = a1[t1 + 1] + 1;
            }
        }
        if (p > 1)
            cout << endl;
        cout << "Case " << p << ":" << endl;
        for (t2 = len1; t2 >= 0; t2--)
        if (a1[t2] == 0)
        {
            cout << num1 << " + " << num2 << " = ";
            for (t2 = len1 - 1; t2 >= 0; t2--)
                 cout<<a1[t2];
            cout << endl;
        }
        else
        {
            cout << num1 << " + " << num2 << " = ";
            for (; t2 >= 0; t2--)
                cout<< a1[t2];
            cout << endl;
        }

    }
    return 0;
}

 

posted @ 2018-10-01 15:56  A-inspire  Views(393)  Comments(0Edit  收藏  举报