• 문제 링크
13777번: Hunt The Rabbit
For each line of input, output the numbers of all envelopes opened, in the order they were opened, until the rabbit is found. Each number must be on the same line separated by a space from the previous number.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int key;
while ((key = read()) != 0) {
int low = 1, high = 50, mid;
do {
sb.append(mid = (low + high) >>> 1).append(" ");
if (mid < key) low = mid + 1;
else high = mid - 1;
} while (mid != key);
sb.append("\n");
}
bw.write(sb.toString());
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 2810 컵홀더 - Greedy / Java (0) | 2023.08.15 |
---|---|
[백준] 4796 캠핑 - Greedy / Java (0) | 2023.08.14 |
[백준] 19592 장난감 경주 - BinarySearch / Java (0) | 2023.08.12 |
[백준] 17124 두 개의 배열 - BinarySearch / Java (0) | 2023.08.11 |
[백준] 13702 이상한 술집 - BinarySearch / Java (0) | 2023.08.10 |
댓글