Notice
Recent Posts
Recent Comments
Link
250x250
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

바닥부터 시작하는 개발 공부

[알고리즘]백준 2164번: 카드2 본문

Algorithm/백준

[알고리즘]백준 2164번: 카드2

Im_light.J 2023. 1. 28. 04:29
728x90
import sys 
from collections import deque 

N = int(sys.stdin.readline().strip())

nums = deque([i for i in range(1,N+1)])

while len(nums)>1:
    nums.popleft()
    num=nums.popleft()
    nums.append(num)
print(nums[0])

숫자 N을 입력받으면 1~N까지의 카드 뭉치를 받는다고 하자 

가장 빠른 카드를 (처음에는 1) 카드 뭉치에서 제외하고 그 다음 카드(2)를 카드 뭉치의 맨 뒤로 보낸다

 

카드 한장이 남을 때까지 이를 반복하고 출력하는 문제 

 

deque 자료형의 popleft와 append를 활용하여 해결 

 

while len(nums)>1:
    nums =nums[1:-1]
    num = nums[0]
    nums = nums[1:-1]
    nums.append(num)
print(nums[0])

while 쪽을 deque안쓰고 해결하려면 위와 같이도 가능할듯 

 

728x90
Comments