#include <cstdio> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class Number { string m_s; size_t m_iLen; public: Number(const string& s) : m_s(s), m_iLen(s.size()) {} int GetDigit(int iPos) { int i = m_iLen - iPos - 1; if (i < 0) { return 0; } return m_s[i] - '0'; } }; int main() { string s0, s1; cin >> s0 >> s1; Number n0(s0); Number n1(s1); int iLen = max(s0.size(), s1.size()); vector<char> vc; int iAcc = 0; for (int i = 0; i < iLen; ++i) { iAcc += n0.GetDigit(i); iAcc += n1.GetDigit(i); int iDigit = iAcc % 10; vc.push_back('0' + iDigit); iAcc /= 10; } if (iAcc != 0) { vc.push_back('0' + iAcc); } for (int i = vc.size() - 1; i >= 0; --i) { cout << vc[i]; } }
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <cstdio> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; class Number { string m_s; size_t m_iLen; public: Number(const string& s) : m_s(s), m_iLen(s.size()) {} int GetDigit(int iPos) { int i = m_iLen - iPos - 1; if (i < 0) { return 0; } return m_s[i] - '0'; } }; int main() { string s0, s1; cin >> s0 >> s1; Number n0(s0); Number n1(s1); int iLen = max(s0.size(), s1.size()); vector<char> vc; int iAcc = 0; for (int i = 0; i < iLen; ++i) { iAcc += n0.GetDigit(i); iAcc += n1.GetDigit(i); int iDigit = iAcc % 10; vc.push_back('0' + iDigit); iAcc /= 10; } if (iAcc != 0) { vc.push_back('0' + iAcc); } for (int i = vc.size() - 1; i >= 0; --i) { cout << vc[i]; } } |