본문 바로가기

python algorithm

백준 1158: 오세푸스 문제

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

 

  해설 (queue를 이용한 원순열 구현)

from collections import deque

queue = deque()

count = 1
n, k = map(int, input().split())
ans = []

for i in range(n):
  queue.append(i + 1)

while queue:
  if count != k:
    queue.append(queue.popleft())
    count += 1

  elif count == k:
    ans.append(queue.popleft())
    count = 1

print('<', end='')
for i in range(len(ans)):
  if i == len(ans) - 1:
    print(ans[i], end='')
  else:
    print(ans[i], end=', ')
print('>', end='')

 

 

'python algorithm' 카테고리의 다른 글

백준 1874번: 스택 수열  (0) 2024.05.19
python 함수 모음  (0) 2024.05.18
백준 10828번: 스택  (0) 2024.05.18