CodingTest/Python
BOJ_2161 : ์นด๋1
Soom_1n
2022. 8. 16. 20:22
2161๋ฒ: ์นด๋1
N์ฅ์ ์นด๋๊ฐ ์๋ค. ๊ฐ๊ฐ์ ์นด๋๋ ์ฐจ๋ก๋ก 1๋ถํฐ N๊น์ง์ ๋ฒํธ๊ฐ ๋ถ์ด ์์ผ๋ฉฐ, 1๋ฒ ์นด๋๊ฐ ์ ์ผ ์์, N๋ฒ ์นด๋๊ฐ ์ ์ผ ์๋์ธ ์ํ๋ก ์์๋๋ก ์นด๋๊ฐ ๋์ฌ ์๋ค. ์ด์ ๋ค์๊ณผ ๊ฐ์ ๋์์ ์นด๋๊ฐ
www.acmicpc.net
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ํ๋ฅผ ๊ตฌํํด์ ์์๊ฐ 1๊ฐ๊ฐ ๋จ์๋๊น์ง ๋ฐ๋ณตํ๋ ๋ฌธ์ ์ด๋ค.
๐ธ ์ฝ๋ ๐ธ
from collections import deque
N = int(input())
que = deque(range(1,N+1))
while len(que) != 1:
print(que.popleft(),end=" ")
# que.append(que.popleft())
que.rotate(-1)
print(que.pop())
๐ธ ์ฝ๋ ํด์ ๐ธ
- ๋ฐํฌ๋ฅผ ์ด์ฉํด์ ๊ตฌํํ๋ค.
- ๊ฐ์ฅ ์ ์์๋ฅผ ๋นผ์ ๋ค์ ์ถ๊ฐํ๋ ๊ฑธ rotate๋ฅผ ์ฌ์ฉํ๋ค.
๐ธ end ๐ธ
- ๋ฐํฌ์ rotate์ append(popleft()) ์ค์ ์๋์ฐจ์ด๊ฐ ์์๊น ํ๋๋ฐ ๋๊ฐ์ ๊ฒ ๊ฐ๋ค.
728x90