#include <iostream> #include <vector> #include <string> using namespace std; istream& operator>> (istream& stream, vector<short>& big_int) { string s; stream >> s; big_int.resize(s.length()); for (int i = 0; i < s.length(); i++) { big_int[s.length()-1-i] = s[i]-'0'; } return stream; } vector<short> operator + (vector<short>& a,vector<short>& b) { if(a.size() < b.size()) { return b + a; } vector<short> sum(a); for (int i = 0; i < b.size(); i++) { sum[i] = b[i]+a[i]; } for (int i = b.size(); i < a.size(); i++) { sum[i] = a[i]; } int rest = 0; int it = 0; while (it<sum.size()) { rest = sum[it]/10; sum[it] = sum[it]%10; it++; if(it<sum.size()) { sum[it] +=rest; } } if(rest > 0) { sum.push_back(rest); } return sum; } ostream& operator << (ostream& stream, vector<short>& big_int) { for (int i = 0; i < big_int.size(); i++) { stream << big_int[big_int.size()-1-i]; } return stream; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<short> a; vector<short> b; cin >> a >> b; vector<short> sum = a+b; cout << sum << 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 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <iostream> #include <vector> #include <string> using namespace std; istream& operator>> (istream& stream, vector<short>& big_int) { string s; stream >> s; big_int.resize(s.length()); for (int i = 0; i < s.length(); i++) { big_int[s.length()-1-i] = s[i]-'0'; } return stream; } vector<short> operator + (vector<short>& a,vector<short>& b) { if(a.size() < b.size()) { return b + a; } vector<short> sum(a); for (int i = 0; i < b.size(); i++) { sum[i] = b[i]+a[i]; } for (int i = b.size(); i < a.size(); i++) { sum[i] = a[i]; } int rest = 0; int it = 0; while (it<sum.size()) { rest = sum[it]/10; sum[it] = sum[it]%10; it++; if(it<sum.size()) { sum[it] +=rest; } } if(rest > 0) { sum.push_back(rest); } return sum; } ostream& operator << (ostream& stream, vector<short>& big_int) { for (int i = 0; i < big_int.size(); i++) { stream << big_int[big_int.size()-1-i]; } return stream; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); vector<short> a; vector<short> b; cin >> a >> b; vector<short> sum = a+b; cout << sum << endl; return 0; } |