[백준] 2164번: 카드2

앙이🐯·2022년 1월 24일
0

알고리즘

목록 보기
12/22

백준 2164번: 카드2

1. 문제 설명

  • 예제 입력:

    6

  • 예제 출력 1

    4

2. 문제 풀이

  • 수가 1개 남을 때 까지 맨 앞의 수를 삭제하고 그 다음 앞의 수를 삭제한 후에 삭제한 수를 맨 뒤에 추가한다.

  • Queue 데이터 추가, 삭제, 검색 메서드

    예외 발생값 리턴
    추가 (enqueue)add(i)offer(i)
    삭제 (dequeue)remove()poll()
    검색 (peek)element()peek()
코드

import java.util.*;

public class No_2164 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		
		Queue<Integer> que=new LinkedList<>();
		
		int N=sc.nextInt();
		
		for(int i=1;i<=N;i++) {
			que.add(i);
		}
		
		while(que.size()>1) {
			que.poll();
			que.add(que.poll());
		}
		
		System.out.println(que.poll());
		
	}

}
실행 결과

0개의 댓글