[백준/파이썬] 2161번 카드1 풀이

업데이트:



문제 정보


풀이

문제

카드를 버리고 옮기는 과정을 반복했을 때 버려지는 카드 순서를 출력하는 문제입니다.

코드

import collections

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

result = []
while True:
    result.append(cards.pop())
    if not cards:
        break
    cards.appendleft(cards.pop())

print(' '.join(map(str, result)))

설명

덱의 끝에서 카드를 꺼내 버리고, 다음 카드를 반대쪽으로 이동시키는 과정을 반복합니다.

버린 순서를 리스트에 모아 공백으로 출력합니다.



댓글남기기