#include<bits/stdc++.h>
using namespace std;
int A[5000 + 8], B[5000 + 8], C[5000 + 8];
int main()
{
string a, b;
cin >> a >> b;
for(int i = 0; i < a.size(); i++)
{
int x = a[a.size() - 1 - i] - '0';
A[i] = x;
}
for(int i = 0; i < b.size(); i++)
{
int x = b[b.size() - 1 - i] - '0';
B[i] = x;
}
int next = 0, lastid = max(b.size(), a.size());
for(int i = 0; i < lastid; i++)
{
int x = A[i] + B[i] + next;
next = 0;
if(x >= 10)
{
x -= 10;
next = 1;
}
C[i] = x;
}
if(next)
{
lastid++;
C[max(a.size(), b.size())] = 1;
}
for(int i = lastid - 1; i >= 0; i--)
cout << C[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 43 44 | #include<bits/stdc++.h> using namespace std; int A[5000 + 8], B[5000 + 8], C[5000 + 8]; int main() { string a, b; cin >> a >> b; for(int i = 0; i < a.size(); i++) { int x = a[a.size() - 1 - i] - '0'; A[i] = x; } for(int i = 0; i < b.size(); i++) { int x = b[b.size() - 1 - i] - '0'; B[i] = x; } int next = 0, lastid = max(b.size(), a.size()); for(int i = 0; i < lastid; i++) { int x = A[i] + B[i] + next; next = 0; if(x >= 10) { x -= 10; next = 1; } C[i] = x; } if(next) { lastid++; C[max(a.size(), b.size())] = 1; } for(int i = lastid - 1; i >= 0; i--) cout << C[i]; cout << '\n'; } |
English