백준 1158 요세푸스 문제

김민영·2023년 1월 11일
0

알고리즘

목록 보기
52/125

과정

  • 원을 일정 간격으로 돌리면서 사람을 하나씩 빼야한다.
  • 큐를 사용해서 일정 간격만큼 pop, append를 진행하고, pop을 해주는 방식으로 진행한다.
    • 큐의 길이가 0이 되면 반복을 중단한다.
from collections import deque
N, K = map(int, input().split())
queue = deque([i for i in range(1, N+1)])
lst = []
while queue:
    for _ in range(K-1):
        a = queue.popleft()
        queue.append(a)
    lst.append(queue.popleft())
    
print("<", end="")
print(lst[0], end="")
for i in lst[1:]:
    print(",", i, end="")
print(">")
  • 출력에 주의하기
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글