-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0415.add_strings.cpp
64 lines (52 loc) · 1.17 KB
/
0415.add_strings.cpp
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
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using std::vector, std::string;
string add_strings(string s1, string s2)
{
std::reverse(s1.begin(), s1.end());
std::reverse(s2.begin(), s2.end());
int m = s1.size();
int n = s2.size();
int t = std::min(m, n);
string res {""};
int c = 0;
for (int i = 0; i < t; ++i) {
int x = s1[i] - '0';
int y = s2[i] - '0';
int s = x + y + c;
c = s / 10;
res.push_back(s % 10 + '0');
}
auto it = s1.begin();
auto end = s1.end();
if (t == m) {
it = s2.begin() + t;
end = s2.end();
} else {
it = s1.begin() + t;
end = s1.end();
}
while (it != end) {
int x = *it - '0';
int s = x + c;
c = s / 10;
res.push_back(s % 10 + '0');
it++;
}
if (c != 0) res.push_back(c + '0');
std::reverse(res.begin(), res.end());
return res;
}
int main () {
#ifdef LOCAL
freopen("0415.in", "r", stdin);
#endif
string s1 {""};
string s2 {""};
while (std::cin >> s1 >> s2) {
std::cout << add_strings(s1, s2) << std::endl;
}
return 0;
}