본문 바로가기
코딩테스트

[백준] 1107번 '리모컨' - Java

by CuckooBird 2023. 8. 18.

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

 

1107번: 리모컨

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

www.acmicpc.net


문제


코드

import java.io.*;
import java.util.*;

public class Main {
	private static final BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	
	private static boolean[] broken = new boolean[10];
	private static int ans = 0;
	
	public static void main(String[] args) throws IOException {
		String n = bf.readLine();
		ans = Math.abs(100 - Integer.parseInt(n));
		
		int m = Integer.parseInt(bf.readLine());
		StringTokenizer st = new StringTokenizer(bf.readLine());
		for(int i = 0 ; i < m ; i++) {
			int t = Integer.parseInt(st.nextToken());
			broken[t] = true;
		}
		
		for(int i = 0 ; i <= 999_999 ; i++) {
			String str = String.valueOf(i);
			int len = str.length();
			boolean isBreak = false;
			for(int j = 0 ; j < len ; j++) {
				if(broken[str.charAt(j) - '0']) {
					isBreak = true;
					break;
				}
			}
			
			if(!isBreak) {
				int min = Math.abs(Integer.parseInt(n) - i) + len;
				ans = Math.min(ans, min);
			}
			
		}
		
		bw.write(ans + "\n");
		
		bf.close();
		bw.flush();
		bw.close();
	}
}