-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd.java
54 lines (53 loc) · 1.14 KB
/
d.java
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
import java.io.*;
import java.util.*;
public class d {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int tc = 1; tc <= T; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String sn = st.nextToken();
int s = Integer.parseInt(st.nextToken());
int[] a = new int[20];
for(int i = 0; i < sn.length(); i++) {
a[i] = sn.charAt(sn.length()-1-i) - '0';
}
int ans[] = new int[20];
for(int i = 0; i < 19; i++) {
if(a[i] == 0) continue;
int sum = 0;
for(int j = 0; j < 20; j++) {
sum += a[j];
}
if(sum <= s) {
break;
}
ans[19-i] = 10-a[i];
a[i] = 0;
int ptr = i+1;
while(ptr < 20) {
a[ptr]++;
if(a[ptr] == 10) {
a[ptr] = 0;
ptr++;
}else {
break;
}
}
}
boolean start = false;
for(int i = 0; i < 20; i++) {
if(ans[i] > 0) {
start = true;
}
if(start) {
System.out.print(ans[i]);
}
}
if(!start) {
System.out.print(0);
}
System.out.println();
}
}
}