-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1700B.cpp
More file actions
53 lines (42 loc) · 1004 Bytes
/
1700B.cpp
File metadata and controls
53 lines (42 loc) · 1004 Bytes
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
#include <bits/stdc++.h>
using namespace std;
string subtract(string a, const string& b) {
int n = a.size();
string res = "";
int carry = 0;
for (int i = n - 1; i >= 0; i--) {
int d1 = a[i] - '0';
int d2 = b[i] - '0' + carry;
if (d1 < d2) {
d1 += 10;
carry = 1;
} else {
carry = 0;
}
res += (d1 - d2) + '0';
}
reverse(res.begin(), res.end());
int i = 0;
while (i < res.size() - 1 && res[i] == '0') i++;
return res.substr(i);
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
string num;
cin >> n >> num;
string ones_n(n, '1');
string target;
if (num > ones_n) {
target = string(n + 1, '1');
num.insert(num.begin(), '0');
} else {
target = string(n, '2');
}
string result = subtract(target, num);
cout << result << '\n';
}
return 0;
}