Queue_요세푸스 문제(1158)
문제
코드 - 덱 풀이
from collections import deque
N,K = map(int,input().split())
arr= deque(list(range(1,N+1)))
cnt = 0
res = []
while arr:
tmp = arr.popleft()
cnt+=1
if cnt == K:
cnt = 0
res.append(tmp)
else:
arr.append(tmp)
print("<"+", ".join(map(str,res))+">")
코드 - 원형큐 풀이
N,K = map(int,input().split())
arr = list(range(N+1))
res = []
size = N+1
front = 0
rear = size-1
tmp = 0
cnt = 0
def enqueue(x):
global rear
if (rear+1)%size != front:
rear = (rear+1)%size
arr[rear] = x
def dequeue():
global front
if front == rear:
print("<"+", ".join(map(str,res))+", "+tmp+">")
sys.exit()
else:
front = (front+1)%size
save = arr[front]
arr[front]=None
return save
while front!=rear:
cnt += 1
tmp = dequeue()
if cnt == K:
cnt = 0
res.append(tmp)
else:
enqueue(tmp)
print("<"+", ".join(map(str,res))+">")