입력 | 출력 |
---|---|
7 3 | <3, 6, 2, 7, 5, 1, 4> |
: 수를 늘여뜨려 놓고, 제일 첫번째에 오는 수만 출력할 수 있도록 구현.
rotate(-1)
을 사용할 수 있음덱
을 이용하여 구현.from collections import deque
n, k = map(int, input().split())
arr = [i for i in range(1, n+1)] # 리스트 컴프리헨션
arr = deque(arr) # 리스트를 덱으로 만들어줌
result = []
while arr: # 리스트에 원소가 존재할동안
for _ in range(k-1):
arr.rotate(-1) # 왼쪽으로 한칸씩 돌리기
# arr.append(arr.popleft()) # k-1번째까지는 pop
result.append(arr.popleft()) # K번째 수를 pop해서 result에 담아준다.
print("<", end="")
for r in result:
if r == result[-1]:
print("{}".format(r), end="")
else:
print("{}, ".format(r), end="")
print(">")