백준

백준 1107 리모컨(Java)

Park DJ 2023. 2. 28. 10:59

https://www.acmicpc.net/problem/1107

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼

www.acmicpc.net


 

Java 코드

 


import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    int m = sc.nextInt();
    boolean[] check = new boolean[10];

    for(int i = 0; i < m; i++) {
      int c = sc.nextInt();
      check[c] = true;
    }

    int min = Math.abs(n - 100);
    for(int i = 0; i <= 999999; i++) {
      String str = String.valueOf(i);
      boolean brk = false;
      for(int j = 0; j < str.length(); j++) {
        if(check[str.charAt(j) - '0'] == true) {
          brk = true;
          break;
        }
      }
      if(brk == false) min = Math.min(min, Math.abs(n - i) + str.length());
    }
    
    System.out.println(min);
  }
}