일상

2164 : 카드2

STUDYING,,, 2021. 8. 12. 01:41
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main4 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in); // 단순 숫자 입력이므로 Scanner을 통하여 입력 받기
		Queue<Integer> queue = new LinkedList<Integer>(); // 큐 자료구조 생성
		
		int n = sc.nextInt(); // 1~n까지의 숫자카드 범위 지정

		for (int i = 1; i <= n; i++) { // queue에 1부터 n까지 순서대로 넣기
			queue.offer(i);
		}

		while (!(queue.size() == 1)) { // queue 사이즈가 1이 될때까지 반복
			queue.poll(); // 제일 윗 장 카드 버리기
			queue.offer(queue.poll()); // 그 다음에 있는 카드를 삭제함과 동시에 제일 마지막 카드로 넣기
		}

		System.out.println(queue.poll()); // 반복문 탈출후 마지막 남은 한 장 숫자 출력

	}

}

'일상' 카테고리의 다른 글

15686 : 치킨 배달  (0) 2021.08.13
3040 : 백설 공주와 일곱 난쟁이  (0) 2021.08.12
2161 : 카드1  (0) 2021.08.12
20001 : 고무오리 디버깅  (0) 2021.08.12
17608 : 막대기  (0) 2021.08.12