• 문제 링크
https://www.acmicpc.net/problem/2161
https://www.acmicpc.net/problem/2164
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
Queue<Integer> q = new ArrayDeque<>();
int n = read();
for (int i = 1; i <= n; i++) q.offer(i);
while (q.size() > 1) {
sb.append(q.poll()).append(' ');
q.offer(q.poll());
}
bw.write(sb.append(q.poll()).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;
}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Queue<Integer> q = new ArrayDeque<>();
int n = read();
for (int i = 1; i <= n; i++) q.offer(i);
while (q.size() > 1) {
q.poll();
q.offer(q.poll());
}
bw.write(String.valueOf(q.poll()));
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' 카테고리의 다른 글
| [백준] 15815 천재 수학자 성필 - Data Structure / Java (0) | 2025.12.03 |
|---|---|
| [백준] 22232 가희와 파일 탐색기 - Data Structure / Java (0) | 2025.12.02 |
| [백준] 34803 문자열 로또 - Data Structure / Java (0) | 2025.11.30 |
| [백준] 32205 네모의 꿈 - Data Structure / Java (0) | 2025.11.29 |
| [백준] 32172 현권이와 신기한 수열 - Data Structure / Java (0) | 2025.11.28 |
댓글