728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static boolean check [];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
int n = Integer.parseInt(br.readLine());
if(n==0){
// 이거때매 틀림
int result = Math.abs(N - 100);
result = Math.min(result,(String.valueOf(N).length()));
System.out.println(result);
}else{
StringTokenizer st = new StringTokenizer(br.readLine()," ");
check = new boolean[10];
for (int i = 0; i < n; i++) {
check[Integer.parseInt(st.nextToken())] = true;
}
System.out.println(solution());
}
}
private static int solution() {
// 완탐..
int result = Math.abs(N - 100); // ++나 --로만 갈 수 있는 최대값.
for (int i = 0; i <= 999999; i++) {
String s = String.valueOf(i);
if(isBroken(s)){
result = Math.min(result,(s.length() + Math.abs(N-i)));
}
}
return result;
}
private static boolean isBroken(String s) {
for (int j = 0; j < s.length(); j++) {
if(check[s.charAt(j)-'0']){
return false;
}
}
return true;
}
}
728x90
반응형