(문제풀이)
백준 2164_카드2
cogito-new
2023. 6. 6. 20:52
- 큐를 이용한 문제 풀이.
큐의 - Linked List, CircularQueue 2가지 방식이 존재한다.
- 큐는 선입선출 구조
package queue;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class bj_2164 {
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
// logic
Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i<N; i++){ // 4
queue.add(i+1); //1, 2, 3, 4
}
// 홀수는 버리고 짝수는 밑으로
int count = 1;
while(queue.size() != 1){
int q = queue.poll();
if(count % 2 == 0){
queue.offer(q);
}
//System.out.println(count + " - > " + queue);
count ++;
}
System.out.println(queue.peek());
}
}
반응형