728x90
반응형

package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Test_2164 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Queue<Integer> queue = new LinkedList<>();
int N = Integer.parseInt(br.readLine());
for(int i = 1; i <= N; i++) {
queue.offer(i);
}
/***** 이 부분을 아래 코드로 바꾸어도 됨 *****/
for(int i = 1; queue.size() != 1 ; i++) {
queue.poll();
queue.offer(queue.poll());
}
System.out.println(queue.poll());
/*******************************************/
}
}
for(int i = 0; i < N; i++) { // i값이 0인 것에 주의!!
if(queue.size() == 1) {
System.out.println(queue.poll());
break;
}
queue.poll();
queue.offer(queue.poll());
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
(백준) 18258번 : 큐 2 - 자바[JAVA] (0) | 2021.09.15 |
---|---|
(백준) 10828번 : 스택 - 자바[JAVA] (0) | 2021.09.15 |
(백준) 4949번 : 균형잡힌 세상 - 자바[JAVA] (0) | 2021.09.14 |
(백준) 11866번 : 요세푸스 문제 0 - 자바[JAVA] (0) | 2021.09.13 |
(백준) 2902번 : KMP는 왜 KMP일까? - 자바[JAVA] (0) | 2021.09.10 |
댓글