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
| #include <bits/stdc++.h>
using namespace std;
string sub(string a, string b) {
if (a == b) return "0";
bool op = false;
if (a.length() < b.length()) {
op = true;
a.swap(b);
} else if (a.length() == b.length() && a < b) {
op = true;
a.swap(b);
}
string res;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int l = max(a.length(), b.length());
for (int i = 0; i < l; i++) {
int an = a[i], bn = i >= b.length() ? '0' : b[i];
if (an < bn) a[i + 1]--, an += 10;
int cn = an - bn;
res.push_back(cn + '0');
}
while (res[res.length() - 1] == '0') res = res.substr(1, res.length() - 1);
reverse(res.begin(), res.end());
return (op ? "-" : "") + res;
}
int main() {
string a, b;
cin >> a >> b;
cout << sub(a, b);
}
|