#include <iostream>
#include <string>
namespace {
using std::cin;
using std::cout;
using std::string;
using std::getline;
using std::swap;
bool add_digits(char & a, char b) noexcept {
a += b - '0';
if (a > '9') {
a -= '9' - '0' + 1;
return true;
} else {
return false;
}
}
void print(string const & s) noexcept {
for (auto const & c : s)
cout << c;
}
}
int main() {
string a, b;
getline(cin, a);
getline(cin, b);
if (a.length() < b.length())
swap(a, b);
size_t pos_a = a.length() - 1;
bool overflow = false;
bool extra_digit = false;
for (size_t k = b.length(); k > 0; --k) {
if (overflow)
++a[pos_a];
overflow = add_digits(a[pos_a], b[k - 1]);
if (pos_a > 0)
--pos_a;
else if (overflow)
extra_digit = true;
}
if (!extra_digit) {
while (overflow && pos_a > 0)
overflow = add_digits(a[pos_a--], '1');
if (overflow)
extra_digit = add_digits(a[0], '1');
}
if (extra_digit)
cout << '1';
print(a);
return 0;
}
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 | #include <iostream> #include <string> namespace { using std::cin; using std::cout; using std::string; using std::getline; using std::swap; bool add_digits(char & a, char b) noexcept { a += b - '0'; if (a > '9') { a -= '9' - '0' + 1; return true; } else { return false; } } void print(string const & s) noexcept { for (auto const & c : s) cout << c; } } int main() { string a, b; getline(cin, a); getline(cin, b); if (a.length() < b.length()) swap(a, b); size_t pos_a = a.length() - 1; bool overflow = false; bool extra_digit = false; for (size_t k = b.length(); k > 0; --k) { if (overflow) ++a[pos_a]; overflow = add_digits(a[pos_a], b[k - 1]); if (pos_a > 0) --pos_a; else if (overflow) extra_digit = true; } if (!extra_digit) { while (overflow && pos_a > 0) overflow = add_digits(a[pos_a--], '1'); if (overflow) extra_digit = add_digits(a[0], '1'); } if (extra_digit) cout << '1'; print(a); return 0; } |
English