#include <iostream> #include <algorithm> #include <vector> #include <map> #include <queue> #include <string> using namespace std; string add(string a, string b) { string result; int ap = a.size() - 1, bp = b.size() - 1; bool mem = 0; while (ap >= 0 || bp >= 0) { int digit = mem; if (ap >= 0) digit += a[ap] - 48; if (bp >= 0) digit += b[bp] - 48; mem = digit / 10; result = "0" + result; result[0] = digit % 10 + 48; ap--; bp--; } if (mem) result = "1" + result; return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b; cin >> a >> b; cout << add(a, b); }
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 | #include <iostream> #include <algorithm> #include <vector> #include <map> #include <queue> #include <string> using namespace std; string add(string a, string b) { string result; int ap = a.size() - 1, bp = b.size() - 1; bool mem = 0; while (ap >= 0 || bp >= 0) { int digit = mem; if (ap >= 0) digit += a[ap] - 48; if (bp >= 0) digit += b[bp] - 48; mem = digit / 10; result = "0" + result; result[0] = digit % 10 + 48; ap--; bp--; } if (mem) result = "1" + result; return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b; cin >> a >> b; cout << add(a, b); } |