#include <iostream> #include <string> using namespace std; int main(){ string a; string b; cin >> a; cin >> b; if (a.size() < b.size()){ string temp = a; a = b; b = temp; } int w[a.size() + 1] = {0}; /*w[0] = 0; for(int i = 0; i < a.size(); i++){ w[i + 1] = (a[i] - 48); }*/ //'0' = 48 int s, x; for(int i = 1; i <= a.size(); i++){ if(i <= b.size()){ x = (b[b.size()-i] - 48); } else { x = 0; } s = w[a.size()+1-i] + (a[a.size()-i] - 48) + x; w[a.size()+1-i] = (s % 10); if(s >= 10){ w[a.size()-i] += (s / 10); } } for(int i = 0; i < a.size() + 1; i++){ if(i == 0 && w[0] == 0){ continue; } cout << w[i]; } cout << "\n"; }
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 | #include <iostream> #include <string> using namespace std; int main(){ string a; string b; cin >> a; cin >> b; if (a.size() < b.size()){ string temp = a; a = b; b = temp; } int w[a.size() + 1] = {0}; /*w[0] = 0; for(int i = 0; i < a.size(); i++){ w[i + 1] = (a[i] - 48); }*/ //'0' = 48 int s, x; for(int i = 1; i <= a.size(); i++){ if(i <= b.size()){ x = (b[b.size()-i] - 48); } else { x = 0; } s = w[a.size()+1-i] + (a[a.size()-i] - 48) + x; w[a.size()+1-i] = (s % 10); if(s >= 10){ w[a.size()-i] += (s / 10); } } for(int i = 0; i < a.size() + 1; i++){ if(i == 0 && w[0] == 0){ continue; } cout << w[i]; } cout << "\n"; } |