본문 바로가기
알고리즘 풀이/자료구조

[백준/파이썬] 11866번: 요세푸스 문제 0

by developer jini 2023. 1. 7.
728x90

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

 

11866번: 요세푸스 문제 0

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net

from collections import deque

n, k = map(int, input().split())
q = deque()
arr = []
for i in range(1, n + 1):
    q.append(i)
# q가 비어있지 않은경우 실행
while q:
    # k-1 번 만큼 앞에 숫자를 뒤로 보내줌
    for _ in range(k - 1):
        x = q.popleft()
        q.append(x)
    arr.append(q.popleft())
    
# 정답 출력    
print("<", end="")
for i in range(n):
    if i == n - 1:
        print(arr[i], end=">")
        break
    print(arr[i], end=", ")
728x90

댓글