Baekjoon - 1107

Tadap·2023년 9월 26일
0

Baekjoon

목록 보기
30/94

문제

Solved.ac Class3

1차시도

public class Main {
	private static final int now = 100;
	private static boolean[] access;
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		access = new boolean[] {true, true, true, true, true, true, true, true, true, true};

		int target = Integer.parseInt(br.readLine());
		int errorSize = Integer.parseInt(br.readLine());

		String[] split = br.readLine().split(" ");

		for (int i = 0; i < errorSize; i++) {
			access[Integer.parseInt(split[i])] = false;
		}

		int min = Math.abs(target - now);

		for (int i = 0; i < 1000000; i++) {
			int numberClicked = solve(i);
			if (numberClicked > 0) {
				int clickPlusMinus = Math.abs(target - i);
				min = Math.min(min, clickPlusMinus + numberClicked);
			}
		}
		System.out.println(min);

	}

	private static int solve(int n) {
		if (n == 0) {
			if (access[0]) {
				return 1;
			} else {
				return 0;
			}
		}
		int count = 0;
		while (n > 0) {
			if (!access[n % 10]) {
				return 0;
			}
			count++;
			n /= 10;
		}
		return count;
	}
}

null point exception

2차시도

public class Main {
	private static final int now = 100;
	private static boolean[] access;
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		access = new boolean[] {true, true, true, true, true, true, true, true, true, true};

		int target = Integer.parseInt(br.readLine());
		int errorSize = Integer.parseInt(br.readLine());

		if (errorSize > 0) {
			String[] split = br.readLine().split(" ");

			for (int i = 0; i < errorSize; i++) {
				access[Integer.parseInt(split[i])] = false;
			}
		}

		int min = Math.abs(target - now);

		for (int i = 0; i < 1000000; i++) {
			int numberClicked = solve(i);
			if (numberClicked > 0) {
				int clickPlusMinus = Math.abs(target - i);
				min = Math.min(min, clickPlusMinus + numberClicked);
			}
		}

		System.out.println(min);

	}

	private static int solve(int n) {
		if (n == 0) {
			if (access[0]) {
				return 1;
			} else {
				return 0;
			}
		}

		int count = 0;

		while (n > 0) {
			if (!access[n % 10]) {
				return 0;
			}
			count++;
			n /= 10;
		}
		return count;
	}
}

망가진 버튼이 없으면 아래 데이터가 안들어오는데 그 부분 예외 처리가 안되어있어서 오류가 났었다.

성공

0개의 댓글