[๋ฐฑ์ค€/python] 11866๋ฒˆ : ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ 0

2021. 5. 12. 16:16

๋ฌธ์ œ

์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ์›์„ ์ด๋ฃจ๋ฉด์„œ ์•‰์•„์žˆ๊ณ , ์–‘์˜ ์ •์ˆ˜ K(≤ N)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์ œ ์ˆœ์„œ๋Œ€๋กœ K๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ œ๊ฑฐํ•œ๋‹ค. ํ•œ ์‚ฌ๋žŒ์ด ์ œ๊ฑฐ๋˜๋ฉด ๋‚จ์€ ์‚ฌ๋žŒ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์›์„ ๋”ฐ๋ผ ์ด ๊ณผ์ •์„ ๊ณ„์†ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด ๊ณผ์ •์€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. ์›์—์„œ ์‚ฌ๋žŒ๋“ค์ด ์ œ๊ฑฐ๋˜๋Š” ์ˆœ์„œ๋ฅผ (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (7, 3)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์€ <3, 6, 2, 7, 5, 1, 4>์ด๋‹ค.

N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง€๋ฉด (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ N ≤ 1,000)

 

์ถœ๋ ฅ

์˜ˆ์ œ์™€ ๊ฐ™์ด ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

from collections import deque
import sys
input = sys.stdin.readline

N, K = input().rstrip().split()
N = int(N)
K = int(K)
people = deque([])
for i in range(1, N + 1):
    people.append(i)

answer = []
while people:
    people.rotate(-K)
    n = people.pop()
    answer.append(n)

#print(answer)
print("<", end='')
for i in answer[:-1]:
    print(i, end=", ")
print(answer[-1], end=">")

deque์˜ rotate๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ์•ž์˜ ์ˆซ์ž๋ฅผ pop()ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

deque์— ๊ด€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

2021.03.19 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ํ (Queue)

 

[ํŒŒ์ด์ฌ/python] ํ (Queue)

ํ(Queue)๋Š” ์Šคํƒ(Stack)๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋ฐ˜๋Œ€์˜ ์„ฑ๊ฒฉ์„ ์ง€๋‹Œ๋‹ค. ์Šคํƒ์€ LIFO(Last In First Out)์˜ ํ˜•ํƒœ๋กœ ๋งˆ์ง€๋ง‰์— ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ฒซ ๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” ๊ตฌ์กฐ์ด๊ณ  ํ๋Š” FIFO(First In First Out)์˜ ํ˜•ํƒœ๋กœ ์ฒซ ๋ฒˆ์งธ..

ye333.tistory.com

โœ” rstrip()์„ ์ด์šฉํ•˜์—ฌ ๊ฐœํ–‰๋ฌธ์ž ์ œ๊ฑฐ ํ›„ split()์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ. ์ˆซ์žํ˜• ์ฒ˜๋ฆฌ๋Š” ๋”ฐ๋กœ ํ•ด์•ผํ•จ.

โœ” rotate ์‹œ ์Œ์ˆ˜๋กœ ์„ค์ •ํ•ด์•ผ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋งจ ์•ž์— ์œ„์น˜ํ•จ.

โœ” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ ์ถœ๋ ฅ ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ๊ธฐ.

728x90

BELATED ARTICLES

more