#include <iostream> using namespace std; int main() { string a; string b; cin >> a >> b; if(a.size() > b.size()){ int diff = a.size() - b.size(); string temp(diff, '0'); b = temp + b; } if(a.size() < b.size()){ int diff = b.size() - a.size(); string temp(diff, '0'); a = temp + a; } string result(a.size(), '0'); int reszta = 0; for(int i = a.size()-1; i > -1; i--){ int aVal = a[i] - '0'; int bVal = b[i] - '0'; int sum = aVal + bVal + reszta; if(sum > 9){ result[i] = (sum % 10) + '0'; reszta = 1; }else{ reszta = 0; result[i] = sum + '0'; } } if(reszta == 1) cout << 1 << '\n'; cout << result << '\n'; 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 | #include <iostream> using namespace std; int main() { string a; string b; cin >> a >> b; if(a.size() > b.size()){ int diff = a.size() - b.size(); string temp(diff, '0'); b = temp + b; } if(a.size() < b.size()){ int diff = b.size() - a.size(); string temp(diff, '0'); a = temp + a; } string result(a.size(), '0'); int reszta = 0; for(int i = a.size()-1; i > -1; i--){ int aVal = a[i] - '0'; int bVal = b[i] - '0'; int sum = aVal + bVal + reszta; if(sum > 9){ result[i] = (sum % 10) + '0'; reszta = 1; }else{ reszta = 0; result[i] = sum + '0'; } } if(reszta == 1) cout << 1 << '\n'; cout << result << '\n'; return 0; } |