본문 바로가기
👩🏻‍💻 Front-end/👾 Algorithm

백준 11866 - 요세푸스 문제 (Python)

by su-no 2022. 5. 27.

풀이 1) list 사용

N, K = map(int, input().split())
people = [i for i in range(1, N+1)]

print("<", end="")

remove_people = K-1
cnt = 0

while(cnt < N):
    cnt += 1
    
    K_cnt = 0
    while(K_cnt < K-1):
        people.append(people[0])
        people.pop(0)
        K_cnt += 1
        
    if cnt >= N:
        print(str(people.pop(0)), end="")
    else:
        print(str(people.pop(0))+", ", end="")
        
print(">")

 

풀이 2) deque 라이브러리 사용

from collections import deque

N, K = map(int, input().split())
people = deque([i for i in range(1, N+1)])

cnt = 0
print("<", end="")
while(people):
    cnt += 1
    for i in range(K-1):
        people.append(people.popleft())
    if cnt == N:
        print(str(people.popleft()), end="")
    else:
        print(str(people.popleft())+", ", end="")
print(">")