HDU-1002 A + B Problem II

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-1002 A + B Problem II相关的知识,希望对你有一定的参考价值。

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


 

经典的大数加法题目。

注意将数字字符转为数字是 减去‘0’ , 将数字转为数字字符是 加上‘0’。

#include <iostream>
using namespace std;

int t, tt, l;

int main(void)
{
    char numa[1001], numb[1001];
    
    void bigsum(char [], char []);
    
    while(cin >> t)
    {
        tt = 0;
        l = t;
        while(t--)
        {
            if(tt <= l)
                tt++;
            cin >> numa >> numb;
            bigsum(numa, numb);
        }
    }
    
    
    return 0;
}


void bigsum(char a[], char b[])
{
    int sum[2000] = {0}, k = 0;
    int len_a = (int)strlen(a);
    int len_b = (int)strlen(b);
    
    for(int i = len_a-1, j = len_b-1; ; i--, j--)
    {
        if(i < 0 && j >= 0)
        {
            sum[k++] += (b[j]-‘0‘);
        }
        else if(i >= 0 && j < 0)
        {
            sum[k++] += (a[i]-‘0‘);
        }
        else if(i < 0 && j < 0)
        {
            break;
        }
        else
        {
            sum[k] += (a[i]-‘0‘) + (b[j]-‘0‘);
            if(sum[k++] >= 10)
            {
                sum[k-1] -= 10;
                sum[k] = 1;
            }
        }
    }
    k--;
    cout << "Case " << tt << ":" << endl;
    cout << a << " + " << b << " = ";
    for( ; k >= 0; k--)
    {
        cout << sum[k];
    }
    cout << endl;
    if(tt != l)
        cout << endl;
    
}

 

以上是关于HDU-1002 A + B Problem II的主要内容,如果未能解决你的问题,请参考以下文章

HDU-1002 A + B Problem II

HDU 1002 A + B Problem II(大整数相加)

HDU 1002: A + B Problem II (大数加法)

hdu::1002 A + B Problem II

HDU 1002 B - A + B Problem II

hdu 1002 A + B Problem II