F006A. Can you add up? P55982


Statement
 

pdf   zip

thehtml

Your task is to write a program that reads pairs of natural numbers and prints their sum.

Warning: the natural numbers that we consider in this problem can be very large (thousands of digits), you must store them in variables of |string| type. The main program is already done; do not modify it:

string x, y; while (cin >> x >> y) cout << sum(x, y) << endl; }

Implement the function

that, given two strings of characters |x| and |y| that represent two natural numbers, returns a string of characters |z| that represents their sum.

To simplify the problem, suppose that neither |x| nor |y| start with ‘|0|’. Besides, suppose that |x| has, at least, the same number of digits than |y|. The number of digits of |z| must be equal to the number of digits of |z| plus one, although it causes that |z| starts with ‘|0|’.

Observations

  • Remember that a string |s| with |n| characters |c| can be declared like this: |string s(n, c);|
  • Remember also that the string operations like |s += ’0’;|, |s1 += s2;| or |s = s1 + s2;| are not allowed.
Public test cases
  • Input

    2 2
    7 8
    1234 1
    9999 1
    1000000000000000000000 1000000000000000000000
    999999999999999999999 99999999999999999999
    10000000000000000000 1
    1870 8428
    100000 1010
    

    Output

    04
    15
    01235
    10000
    02000000000000000000000
    1099999999999999999998
    010000000000000000001
    10298
    0101010
    
  • Input

    999999999999999999999999999999999999999999999999999999999999999999999999999999999 2
    

    Output

    1000000000000000000000000000000000000000000000000000000000000000000000000000000001
    
  • Information
    Author
    Professorat de P1
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++