[백준/파이썬] 2164번 카드2 풀이

업데이트:



문제 정보


풀이

문제

1..N 카드에서 맨 위 카드를 버리고, 다음 카드를 맨 아래로 옮기는 과정을 반복했을 때 마지막 카드 번호를 구하는 문제입니다.

코드

import collections

n = int(input())
cards = collections.deque([i for i in range(n, 0, -1)])

while True:
    tmp = cards.pop()
    if not cards:
        print(tmp)
        break
    cards.appendleft(cards.pop())

설명

deque를 사용해 끝 원소 제거/반대편 삽입 연산을 반복하면 요구한 규칙을 그대로 시뮬레이션할 수 있습니다.



댓글남기기