카드 2

yongju·2022년 12월 8일
0

BAEKJOON

목록 보기
20/40
post-thumbnail

❓문제

https://www.acmicpc.net/problem/2164

❗문제 정리

스택(리스트)로 처리하면 시간초과가난다.
큐로 풀어야하는 문제!

📑코드

from collections import deque
n=int(input())
cards=deque([x for x in range(1, n+1)])

if n==1:
    print(1)
else:
    while len(cards)!=1:
        cards.popleft()
        cards.append(cards.popleft())

    for card in cards:
        print(card)

📝코드 설명

n=int(input())
cards=deque([x for x in range(1, n+1)])

필요한 파라미터 입력받기
n(int) : 카드의 마지막 번호이자 전체 카드 수
cards :1부터 n까지의 카드

if n==1:
    print(1)

카드가 1개라면 1을 출력.
99%에서 실패했다면, 이 경우를 고려하지 못한것.

    while len(cards)!=1:
        cards.popleft()
        cards.append(cards.popleft())

카드가 1장이 남을 때까지(len(cards)!=1) 왼쪽을 버리고, 다시 왼쪽에 있는걸 뽑아서 뒤로 붙여준다.

 for card in cards:
        print(card)

카드의 수가 1장이 되면 마지막 남은 카드를 출력

🎖제출 결과

💡insight

profile
AI dev

0개의 댓글