백준 1158 요세푸스 파이썬

2021. 9. 25. 17:24

출력 연습하는 문제인가?

아니면 내가 이상하게 풀었나..?

큐 사용해서 넣고 빼는건 쉬웠는데, 외려 출력문 만드는데 시간을 더 썼다.

 

from collections import deque
N, K = map(int, input().split())

table = deque()
answer = []

for i in range(1, N + 1):
    table.append(i)

print("<", end="")
while table:
    for _ in range(K):
        table.append(table.popleft())
    if len(table) == 1:
        print(table.pop(), end="")
    else:
        print(table.pop(), end=", ")
print(">", end="")

 

다른사람 풀이 보니까

deque를 안쓰고 index를 높이다 길이를 넘어가면 %로 다시 앞으로 당기는 과정을 통해 풀었다.

https://www.byfuls.com/programming/read?id=48 

 

python 백준 알고리즘, 1158:요세푸스 문제

python 백준 알고리즘, 1158:요세푸스 문제

www.byfuls.com

 

BELATED ARTICLES

more