1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Problem name: A + B
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>

int char_to_int(char character)
{
    return static_cast<int>(character) - 48;
}

char int_to_char(int digit)
{
    return static_cast<char>(digit + 48);
}

std::vector<int> represent_number(const std::string &str, int length)
{
    std::vector<int> result(length, 0);
    int string_size = str.size();
    int index = length - string_size;
    for (const char &c : str)
    {
        result[index] = char_to_int(c);
        ++index;
    }
    return result;
}

std::vector<char> sum_vectors(const std::vector<int> &left, const std::vector<int> &right)
{
    std::vector<char> result;
    int length = left.size();
    int carry = 0;
    for (int i = length - 1; i >= 0; --i)
    {
        int sum = left[i] + right[i] + carry;
        int digit = sum % 10;
        carry = int(sum / 10);
        result.push_back(int_to_char(digit));
    }
    if (carry > 0)
    {
        result.push_back(int_to_char(carry));
    }
    return result;
}

bool isSmaller(std::string str1, std::string str2)
{
    int n1 = str1.length(), n2 = str2.length();
    if (n1 < n2)
        return true;
    if (n2 < n1)
        return false;

    for (int i = 0; i < n1; i++)
        if (str1[i] < str2[i])
            return true;
        else if (str1[i] > str2[i])
            return false;

    return false;
}

std::string findDiff(std::string str1, std::string str2)
{
    if (isSmaller(str1, str2))
        swap(str1, str2);
    std::string str = "";
    int n1 = str1.length(), n2 = str2.length();
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());
    int carry = 0;
    for (int i = 0; i < n2; i++)
    {
        int sub = ((str1[i] - '0') - (str2[i] - '0') - carry);
        if (sub < 0)
        {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;
        str.push_back(sub + '0');
    }
    for (int i = n2; i < n1; i++)
    {
        int sub = ((str1[i] - '0') - carry);
        if (sub < 0)
        {
            sub = sub + 10;
            carry = 1;
        }
        else
            carry = 0;
        str.push_back(sub + '0');
    }
    reverse(str.begin(), str.end());
    return str;
}

std::string findSum(std::string str1, std::string str2)
{
    int length = std::max(str1.size(), str2.size());
    auto left = represent_number(str1, length);
    auto right = represent_number(str2, length);
    auto sum = sum_vectors(left, right);
    reverse(sum.begin(), sum.end());
    return std::string(sum.begin(), sum.end());
}

int main()
{
    std::string a, b;
    std::cin >> a;
    std::cin >> b;
    std::string unsigned_a = a.substr(1, a.size());
    std::string unsigned_b = b.substr(1, b.size());
    if (a[0] == '-' && b[0] == '-')
    {
        std::cout << '-' << findSum(unsigned_a, unsigned_b) << "\n";
    }
    else if (a[0] != '-' && b[0] != '-')
    {
        std::cout << findSum(a, b) << "\n";
    }
    else if (a[0] == '-' && b[0] != '-')
    {
        if (!isSmaller(unsigned_a, b))
        {
            std::cout << '-';
        }
        std::cout << findDiff(b, unsigned_a) << "\n";
    }
    else if (a[0] != '-' && b[0] == '-')
    {
        if (!isSmaller(unsigned_b, a))
        {
            std::cout << '-';
        }
        std::cout << findDiff(a, b.substr(1, b.size())) << "\n";
    }
    return EXIT_SUCCESS;
}