-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMain.java
61 lines (50 loc) · 1.63 KB
/
Main.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
55
56
57
58
59
60
61
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
import static java.lang.Math.abs;
import static java.lang.Math.min;
public class Main {
private static int N;
private static int M;
private static boolean[] broken;
private static int res;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
M = Integer.parseInt(br.readLine());
broken = new boolean[10];
Arrays.fill(broken, false);
if (M != 0) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < M; i++) {
int tmp = Integer.parseInt(st.nextToken());
broken[tmp] = true;
}
}
res = abs(N - 100); // +, - 만으로 이동
for (int channel = 0; channel <= 1000000; channel++) {
int cnt = ButtonCount(channel); // 채널입력이 가능: 자릿수, 불가능: -1
if (cnt == -1) continue;
cnt += abs(N - channel);
res = min(res, cnt);
}
System.out.println(res);
}
public static int ButtonCount(int num) {
int cnt = 0;
if (num == 0) {
if (broken[0]) return -1;
return 1;
}
while (num > 0) {
int btn = num % 10;
if (broken[btn]) return -1;
cnt += 1;
num /= 10;
}
return cnt;
}
}