-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0057.cpp
83 lines (79 loc) · 2 KB
/
0057.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 0057.无线OSS-高精度整数加法
// keywords:模拟加法、模拟减法
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string add(string a, string b)
{
string r = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if (a.size() < b.size()) a.append(b.size()-a.size(), '0');
else if (a.size() > b.size()) b.append(a.size()-b.size(), '0');
int c = 0;
for(int i = 0; i < a.size(); i++)
{
int t = a[i] + b[i] - '0'*2 + c;
r.append(1, t % 10+'0');
c = t / 10;
}
if(c) r.append(1, c+'0');
reverse(r.begin(), r.end());
return r;
}
string sub(string a, string b)
{
string r = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if(a.size() < b.size()) a.append(b.size()-a.size(), '0');
else if(a.size() > b.size()) b.append(a.size()-b.size(), '0');
for(int i = 0; i < b.size(); i++)
{
int t = a[i] - b[i];
if (t < 0) {
t += 10;
int j = i;
for(; a[j] == '0'; j ++) {
a[i] = '9';
}
a[j] -= 1;
}
r.append(1, t + '0');
}
reverse(r.begin(), r.end());
while(r[0] == '0') r.erase(0, 1);
return r;
}
int main()
{
string s1, s2;
while(cin >> s1 >> s2)
{
string r;
if(s1[0] != '-' && s2[0] != '-') {
r = add(s1, s2);
} else if(s1[0] == '-' && s2[0] == '-') {
r = add(s1.substr(1), s2.substr(1));
r.insert(0, 1, '-');
} else {
string t1, t2;
if(s1[0] == '-') {
t1 = s1.substr(1);
t2 = s2;
} else {
t1 = s2.substr(1);
t2 = s1;
}
if (t1 > t2 || t1.size() > t2.size()) {
r = sub(t1, t2);
r.insert(0, 1, '-');
}else {
r = sub(t2, t1);
}
}
cout << r << endl;
}
return 0;
}