- 큐를 이용한 문제 풀이.
큐의 - 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());
}
}
반응형
'(문제풀이)' 카테고리의 다른 글
프로그래머스 - 푸드 파이트 대회(Java) (0) | 2023.08.13 |
---|---|
프로그래머스 - 크기가 작은 부분 문자열(java) (0) | 2023.08.13 |
백준 9012_괄호 (0) | 2023.06.05 |
백준 11728_배열 합치기 (0) | 2023.06.05 |
프로그래머스(java) : 음양 더하기 (0) | 2022.11.15 |