#include <iostream> int main() { std::ios_base::sync_with_stdio(0); std::string a,b,C; std::cin >> a >> b; std::string A(a.rbegin(), a.rend()); std::string B(b.rbegin(), b.rend()); while (A.length() < B.length()) A.push_back('0'); while (A.length() > B.length()) B.push_back('0'); int cc = 0; for (int i=0;i<A.length();++i) { int a = A[i]-'0'; int b = B[i]-'0'; int c = (a+b+cc)%10; cc = (a+b+cc)/10; C.push_back(static_cast<char>(c+'0')); } if (cc > 0) C.push_back(static_cast<char>(cc+'0')); while (C.length() > 1 && C.back() == '0') C.pop_back(); std::string c(C.rbegin(), C.rend()); std::cout << c << std::endl; 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 | #include <iostream> int main() { std::ios_base::sync_with_stdio(0); std::string a,b,C; std::cin >> a >> b; std::string A(a.rbegin(), a.rend()); std::string B(b.rbegin(), b.rend()); while (A.length() < B.length()) A.push_back('0'); while (A.length() > B.length()) B.push_back('0'); int cc = 0; for (int i=0;i<A.length();++i) { int a = A[i]-'0'; int b = B[i]-'0'; int c = (a+b+cc)%10; cc = (a+b+cc)/10; C.push_back(static_cast<char>(c+'0')); } if (cc > 0) C.push_back(static_cast<char>(cc+'0')); while (C.length() > 1 && C.back() == '0') C.pop_back(); std::string c(C.rbegin(), C.rend()); std::cout << c << std::endl; return 0; } |