#include <iostream> #include <algorithm> using namespace std; int main() { string a, b, result; cin >> a >> b; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int longer = max(a.size(), b.size()); int carry = 0; int res = 0; for(int i=0;i<longer;i++) { int a_digit = 0; if(i<a.size()) a_digit = a[i]-'0'; int b_digit = 0; if(i<b.size()) b_digit = b[i]-'0'; res = a_digit+b_digit+carry; if(res > 9) { carry = 1; res %= 10; } else carry = 0; result.push_back(char('0'+res)); } if(carry) result.push_back('1'); reverse(result.begin(), result.end()); cout << result; 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 | #include <iostream> #include <algorithm> using namespace std; int main() { string a, b, result; cin >> a >> b; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int longer = max(a.size(), b.size()); int carry = 0; int res = 0; for(int i=0;i<longer;i++) { int a_digit = 0; if(i<a.size()) a_digit = a[i]-'0'; int b_digit = 0; if(i<b.size()) b_digit = b[i]-'0'; res = a_digit+b_digit+carry; if(res > 9) { carry = 1; res %= 10; } else carry = 0; result.push_back(char('0'+res)); } if(carry) result.push_back('1'); reverse(result.begin(), result.end()); cout << result; return 0; } |