#include <iostream> using namespace std; string a,b, wyn; int r,len, len2; int main() { cin >> a >> b; if (a.length() < b.length()) { swap(a,b); } len = a.length(); len2 = a.length() - b.length(); for (int i = 0; i < len2; i++) b = "0" + b; wyn = ""; int x,y,z; for (int i = len - 1; i >= 0; i--) { x = (int)a[i] - 48; y = (int)b[i] - 48; z = (x + y + r) % 10; wyn = to_string(z) + wyn; if (x + y + r > 9) r = 1; else r = 0; } if (r == 1) wyn = "1" + wyn; cout << wyn; }
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 | #include <iostream> using namespace std; string a,b, wyn; int r,len, len2; int main() { cin >> a >> b; if (a.length() < b.length()) { swap(a,b); } len = a.length(); len2 = a.length() - b.length(); for (int i = 0; i < len2; i++) b = "0" + b; wyn = ""; int x,y,z; for (int i = len - 1; i >= 0; i--) { x = (int)a[i] - 48; y = (int)b[i] - 48; z = (x + y + r) % 10; wyn = to_string(z) + wyn; if (x + y + r > 9) r = 1; else r = 0; } if (r == 1) wyn = "1" + wyn; cout << wyn; } |