Problem Solving/Baekjoon
[백준] 13777 Hunt The Rabbit - BinarySearch / Java
graycode
2023. 8. 13. 15:34
• 문제 링크
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;
}
}