Baekjoon - 16953

Tadap·2023년 11월 9일
0

Baekjoon

목록 보기
79/94

문제

Solved.ac Class4

1차시도

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int answer = -1;

		String[] split = br.readLine().split(" ");
		int startPoint = Integer.parseInt(split[0]);
		int target = Integer.parseInt(split[1]);

		Queue<Info> queue = new LinkedList<>();
		queue.add(new Info(startPoint, 1));

		while (!queue.isEmpty()) {
			Info visit = queue.remove();
			int now = visit.now;
			int count = visit.count;

			if (now == target) {
				answer = count;
				break;
			}

			int makeDouble = now * 2;
			int addNumber = now * 10 + 1;

			if (makeDouble <= target) {
				queue.add(new Info(makeDouble, count + 1));
			}

			if (addNumber <= target) {
				queue.add(new Info(addNumber, count + 1));
			}

		}

		System.out.println(answer);

	}

	static class Info {
		private final int now;
		private final int count;

		public Info(int now, int count) {
			this.now = now;
			this.count = count;
		}
	}
}

실패

2차시도

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		long answer = -1;

		String[] split = br.readLine().split(" ");
		int startPoint = Integer.parseInt(split[0]);
		int target = Integer.parseInt(split[1]);

		Queue<Info> queue = new LinkedList<>();
		queue.add(new Info(startPoint, 1));

		while (!queue.isEmpty()) {
			Info visit = queue.remove();
			long now = visit.now;
			long count = visit.count;

			if (now == target) {
				answer = count;
				break;
			}

			long makeDouble = now * 2;
			long addNumber = now * 10 + 1;

			if (makeDouble <= target) {
				queue.add(new Info(makeDouble, count + 1));
			}

			if (addNumber <= target) {
				queue.add(new Info(addNumber, count + 1));
			}

		}

		System.out.println(answer);

	}

	static class Info {
		private final long now;
		private final long count;

		public Info(long now, long count) {
			this.now = now;
			this.count = count;
		}
	}
}

int자료형의 범위가 문제였다.
int 범위는 2,147,483,647인데 10910^91,000,000,000 이다 여기서 1을 뒤에 붙이면 범위를 넘어가 오류를 낸다.

성공

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		long answer = -1;

		String[] split = br.readLine().split(" ");
		int startPoint = Integer.parseInt(split[0]);
		int target = Integer.parseInt(split[1]);

		Queue<Info> queue = new LinkedList<>();
		queue.add(new Info(startPoint, 1));

		while (!queue.isEmpty()) {
			Info visit = queue.remove();
			long now = visit.now;
			int count = visit.count;

			if (now == target) {
				answer = count;
				break;
			}

			long makeDouble = now * 2;
			long addNumber = now * 10 + 1;

			if (makeDouble <= target) {
				queue.add(new Info(makeDouble, count + 1));
			}

			if (addNumber <= target) {
				queue.add(new Info(addNumber, count + 1));
			}

		}

		System.out.println(answer);

	}

	static class Info {
		private final long now;
		private final int count;

		public Info(long now, int count) {
			this.now = now;
			this.count = count;
		}
	}

}

정확히는 위처럼 now 변수만 long으로 취해주면 된다.

0개의 댓글