#include <iostream> #include <algorithm> #include <string> using namespace std; string dodaj(string a, string b) { string w; int amp = 0; if (a.size() < b.size()) swap(a,b); for (int i = 0; i < b.size(); i++) { w += char(a[a.size() - 1 - i] + b[b.size() - 1 - i] - '0' + amp); if (w[i] > '9') { // cout << "aaaa"; w[i] = w[i] - 10; amp = 1; } else { amp = 0; } } for (int i = b.size(); i < a.size(); i++) { w += char(a[a.size() - 1 - i] + amp); if (w[i] > '9') { w[i] = w[i] - 10; amp = 1; } else { amp = 0; } } if (amp == 1) w+= "1"; reverse(w.begin(),w.end()); return w; } int main() { string a, b; cin >> a >> b; cout << dodaj(a,b); 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 | #include <iostream> #include <algorithm> #include <string> using namespace std; string dodaj(string a, string b) { string w; int amp = 0; if (a.size() < b.size()) swap(a,b); for (int i = 0; i < b.size(); i++) { w += char(a[a.size() - 1 - i] + b[b.size() - 1 - i] - '0' + amp); if (w[i] > '9') { // cout << "aaaa"; w[i] = w[i] - 10; amp = 1; } else { amp = 0; } } for (int i = b.size(); i < a.size(); i++) { w += char(a[a.size() - 1 - i] + amp); if (w[i] > '9') { w[i] = w[i] - 10; amp = 1; } else { amp = 0; } } if (amp == 1) w+= "1"; reverse(w.begin(),w.end()); return w; } int main() { string a, b; cin >> a >> b; cout << dodaj(a,b); return 0; } |