#include <string> #include <iostream> using namespace std; string a, b, sum=""; int iSum, c=0; int main() { cin >> a; cin >> b; int maxLength = (a.size() >= b.size()) ? a.size() : b.size(); if (maxLength==0) maxLength=1; a.insert(0,maxLength-a.size(),'0'); b.insert(0,maxLength-b.size(),'0'); for (int i=maxLength-1; i>=0; i--) { iSum = int(a[i]) + int(b[i]) - 96 + c; sum.insert(0,1,char(iSum % 10 + 48)); c = iSum / 10; } if (c==1) sum.insert(0,1,char(c + 48)); cout << sum; 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 <string> #include <iostream> using namespace std; string a, b, sum=""; int iSum, c=0; int main() { cin >> a; cin >> b; int maxLength = (a.size() >= b.size()) ? a.size() : b.size(); if (maxLength==0) maxLength=1; a.insert(0,maxLength-a.size(),'0'); b.insert(0,maxLength-b.size(),'0'); for (int i=maxLength-1; i>=0; i--) { iSum = int(a[i]) + int(b[i]) - 96 + c; sum.insert(0,1,char(iSum % 10 + 48)); c = iSum / 10; } if (c==1) sum.insert(0,1,char(c + 48)); cout << sum; return 0; } |