RUBY

[백준] 요세푸스 문제 본문

PS/BOJ

[백준] 요세푸스 문제

RUBY_루비 2020. 9. 28. 23:59

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

분류:: 큐, 원형큐

 

1. 문제 이해 및 해결과정

- 원형 큐로 해결

 

2. 풀이방법

  1.원형 큐

#요세푸스 문제
#https://www.acmicpc.net/problem/1158
import sys
from collections import deque
sys.stdin = open("input.txt","r")
n,k=map(int,input().split())
q=deque(i for i in range(1,n+1))
cnt=0
result=[]

while q:
    q.rotate(-(k-1))
    result.append(q.popleft())

print("<",end='')
for i in range(len(result)):
    if i!=len(result)-1:
        print("%d," %result[i],end=' ')
    else:
        print("%d>" %result[i])

 

3. 오답원인

 

4. 알게된 점

 deque.rotate()
# 1만큼 오른쪽으로 밀 때
q = deque(['a', 'b', 'c', 'd', 'e'])
deq.rotate(1)

##['e', 'a', 'b', 'c', 'd']


# 1만큼 왼쪽으로 밀 때
q = deque(['a', 'b', 'c', 'd', 'e'])
deq.rotate(-1)

##['b', 'c', 'd', 'e', 'a']

'PS > BOJ' 카테고리의 다른 글

[백준] 스택 수열  (0) 2020.09.28
[백준] 단어뒤집기  (0) 2020.09.28
[백준] 카드 구매하기  (0) 2020.09.28
[백준] 에디터  (0) 2020.09.28
[백준] 격자상의 경로  (0) 2020.09.27
Comments