๐ Algorithm/๋ฐฑ์ค
[๋ฐฑ์ค] 11866๋ฒ: ์์ธํธ์ค ๋ฌธ์ 0(Python)
Dhey
2022. 3. 31. 17:25
๋ฐ์ํ
๋ฌธ์

11866๋ฒ: ์์ธํธ์ค ๋ฌธ์ 0
์ฒซ์งธ ์ค์ N๊ณผ K๊ฐ ๋น ์นธ์ ์ฌ์ด์ ๋๊ณ ์์๋๋ก ์ฃผ์ด์ง๋ค. (1 ≤ K ≤ N ≤ 1,000)
www.acmicpc.net
์ ์ถ ์ฝ๋
from collections import deque
N, K = map(int, input().split())
q = deque()
for i in range(1, N+1):
q.append(i)
li = []
while q:
for i in range(K-1):
q.append(q.popleft())
li.append(q.popleft())
print("<", end="")
for i in range(len(li)-1):
print("%d" % li[i], end=", ")
print(li[-1], end="")
print(">")
์ด ๋ฌธ์ ๋ deque๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
popleft()๋ ๊ฐ์ฅ ์ผ์ชฝ์ ์์๋ฅผ ์ ๊ฑฐํด์ฃผ๋ฉฐ ์ ๊ฑฐํ ๊ฐ์ return ํด์ค๋ค.
๋ฐ๋ผ์ ์์ q.append(q.popleft())๋ '๊ฐ์ฅ ์ผ์ชฝ ์์๋ฅผ ์ ๊ฑฐํ์ฌ return ํ ํ, return ๋ ๊ฐ์ ๋งจ ๋ค์ ์ถ๊ฐํ๋ค'๋ ์๋ฏธ์ด๋ค.
์ด๋ฐ ๋ฐฉ์์ผ๋ก K๋ฒ์ 1๋ฒ ๊ผด๋ก ์์๋ฅผ ์ ๊ฑฐํ๊ณ ์ถ๋ ฅํ ์๋ก์ด ๋ฆฌ์คํธ์ ์์๋๋ก ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
๋ฐ์ํ