HDU1002 A + B problem II

Source: Internet
Author: User

Problem Description
I have a very simple problem for you. Given 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 both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed 1000.

Output
For the test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.

Sample Input
2
1 2
112233445566778899 998877665544332211

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

Author
Ignatius.l

Recommend
We have carefully selected several similar problems for YOU:1004 1005 1009 1020 1010

Because the problem is not brushed for more than a year, so this is a rehabilitation training ...

Test instructions: Sum of two large integers
Idea: It is in accordance with mathematical methods, from the single-digit beginning to add, if the full 10 into a, simulated childhood play with the Abacus to do add and subtract.

Where CE once, found that the operation of a string to use the string header, Ah, and CString is not a header file said.

#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std;
    int main () {string A, B;
    int na[8000],nb[8000],sum[8000],pre;
    int num;
    CIN >> Num;
        for (int j = 1;j<=num;j++) {memset (sum,0,sizeof (sum));
        memset (na,0,sizeof (NA));
        memset (nb,0,sizeof (NB));
        Cin >> a >> b;
        Pre = 0;
        int Lena = A.length ();
        int lenb = B.length ();
        for (int i = 0;i<lena;i++) na[lena-i-1] = a[i]-' 0 ';
        for (int i = 0;i<lenb;i++) nb[lenb-i-1] = b[i]-' 0 '; int Lenx = Lena > LenB?
        Lena:lenb;
            for (int i = 0;i < Lenx; i++) {sum[i] = Na[i] + nb[i] + pre;
            Pre = 0;
                if (sum[i]>=) {pre = 1;
            Sum[i] = sum[i]%10;
        }} cout << "case" << J << ":" <<endl; cout <<
        A << "+" << b << "=";
        for (int i = lenx-1;i>=0;i--) cout<<sum[i];
        cout<<endl;
    if (j!=num) cout<<endl;
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.