๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 2164 - ์นด๋“œ2

by YWTechIT 2021. 5. 4.
728x90

๐Ÿ“Œ ๋ฐฑ์ค€ 2164 - card2

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

๋‹จ์ˆœํ•˜๊ฒŒ ๋ฌธ์ œ์— ๋‚˜์™€์žˆ๋Š” ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์ œ์ผ ์œ„์— ์žˆ๋Š” ์นด๋“œ๋ฅผ ์ œ์ผ ์•„๋ž˜์— ์žˆ๋Š” ์นด๋“œ ๋ฐ‘์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์€ deque์˜ popleft()๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. deque๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  pop(0)์„ ์‚ฌ์šฉํ•˜๊ฒŒ๋˜๋ฉด, ๋ฒ”์œ„๊ฐ€ 500,000์ •๋„๋กœ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ ํŒ์ •์ด ๋‚œ๋‹ค. ๋”ฐ๋ผ์„œ deque๋ฅผ ์ ๊ทน ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.

 

while๋ฌธ์˜ ๋ฒ”์œ„๋ฅผ card๋กœ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋” ์ด์ƒ ๋บผ ์›์†Œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค. len(cards)์˜ ๊ธธ์ด๊ฐ€ 1์ดํ•˜๋กœ ์ค„์–ด๋“ค ๋•Œ๋กœ ์„ค์ •ํ•˜์ž.

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

n = int(input())
cards = deque(range(1, n+1))

while len(cards) > 1:
    cards.popleft()
    cards.append(cards.popleft())

print(''.join(map(str, cards)))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€