#include <iostream> #include <vector> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <tuple> #include <random> #include <cstdlib> #include <time.h> #include <limits> #include <set> #include <map> #include <queue> #include <cmath> #include <unordered_set> #include <unordered_map> #include <list> #include <fstream> #define INF 2'000'000'000'000 #define ll long long int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::string a; std::string b; std::cin >> a >> b; int i = a.size() - 1; int j = b.size() - 1; std::vector<int> out; int carry = 0; while (i >= 0 && j >= 0) { out.push_back((a[i] - '0' + b[j] - '0' + carry) % 10); carry = (a[i] - '0' + b[j] - '0' + carry) / 10; i--; j--; } while (i >= 0) { out.push_back((a[i] - '0' + carry) % 10); carry = (a[i] - '0' + carry) / 10; i--; } while (j >= 0) { out.push_back((b[j] - '0' + carry) % 10); carry = (b[j] - '0' + carry) / 10; j--; } if (carry != 0) { out.push_back(carry); } for (int i = out.size() - 1; i >= 0; i--) { std::cout << out[i]; } }
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 <vector> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <tuple> #include <random> #include <cstdlib> #include <time.h> #include <limits> #include <set> #include <map> #include <queue> #include <cmath> #include <unordered_set> #include <unordered_map> #include <list> #include <fstream> #define INF 2'000'000'000'000 #define ll long long int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); std::string a; std::string b; std::cin >> a >> b; int i = a.size() - 1; int j = b.size() - 1; std::vector<int> out; int carry = 0; while (i >= 0 && j >= 0) { out.push_back((a[i] - '0' + b[j] - '0' + carry) % 10); carry = (a[i] - '0' + b[j] - '0' + carry) / 10; i--; j--; } while (i >= 0) { out.push_back((a[i] - '0' + carry) % 10); carry = (a[i] - '0' + carry) / 10; i--; } while (j >= 0) { out.push_back((b[j] - '0' + carry) % 10); carry = (b[j] - '0' + carry) / 10; j--; } if (carry != 0) { out.push_back(carry); } for (int i = out.size() - 1; i >= 0; i--) { std::cout << out[i]; } } |