#include <bits/stdc++.h> #define ll long long using namespace std; int main() { std::ios::sync_with_stdio(false); string A, B, C; cin >> A >> B; reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); C.reserve(max(A.size(), B.size()) + 2); ll carry = 0, c; for(ll i = 0; i < max(A.size(), B.size()); i++) { c = carry; if(i < A.size()) c += (A[i] - '0'); if(i < B.size()) c += (B[i] - '0'); carry = c / 10; C.push_back((c % 10) + '0'); } if(carry > 0) C.push_back(carry + '0'); reverse(C.begin(), C.end()); cout << C << "\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 | #include <bits/stdc++.h> #define ll long long using namespace std; int main() { std::ios::sync_with_stdio(false); string A, B, C; cin >> A >> B; reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); C.reserve(max(A.size(), B.size()) + 2); ll carry = 0, c; for(ll i = 0; i < max(A.size(), B.size()); i++) { c = carry; if(i < A.size()) c += (A[i] - '0'); if(i < B.size()) c += (B[i] - '0'); carry = c / 10; C.push_back((c % 10) + '0'); } if(carry > 0) C.push_back(carry + '0'); reverse(C.begin(), C.end()); cout << C << "\n"; return 0; } |