Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::string sum(const std::string& a, const std::string& b);
-
- int main() {
- std::string a;
- std::cin >> a;
- std::string b;
- std::cin >> b;
-
- std::cout << sum(a, b);
-
- return 0;
- }
-
- std::string sum(const std::string& a, const std::string& b) {
- std::string sum;
- int i = a.size() - 1;
- int j = b.size() - 1;
- int one = 0;
- while ((i >= 0) || (j >= 0)) {
- char ai = (i >= 0) ? a[i] : '0';
- char bj = (j >= 0) ? b[j] : '0';
- int s = static_cast<int>(ai - 48) +
- static_cast<int>(bj - 48) + one;
- if (s >= 10) {
- sum = static_cast<char>(s - 10 + 48) + sum;
- one = 1;
- } else {
- sum = static_cast<char>(s + 48) + sum;
- one = 0;
- }
- --i;
- --j;
- }
- if (one == 1)
- sum = '1' + sum;
- return sum;
- }
-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement