๋ฐฑ์ค 2164๋ฒ
- ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ ์ฝ๋
n = int(input())
card = []
for i in range(1, n + 1):
card.append(i)
def card2(a):
while len(a) != 1:
a.pop(0)
left = a.pop(0)
a.append(left)
#card2(a)
return a[0]
print(card2(card))
โ ์ฐพ์๋ณด๋๊น ์๊ฐ๋ณต์ก๋ ๋๋ฌธ์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ณด์๋ค. ๊ตฌ๊ธ๋ง์ ํด๋ณด๋ deque์๋๋ฉด ์ง์ ๊ท์น์ ์ฐพ์์ ํด๊ฒฐํ๋๋ฐ, deque๊ฐ ๋ญ์ง ๋ชฐ๋๊ธฐ์ ์ด๋ฒ ๊ธฐํ์ ์ ๋ฆฌํด๋ณด๊ณ ์ ํ๋ค.
deque
Stack ์ด๋ queue ์ฒ๋ผ ํ ๋ฐฉํฅ์์ ์ฝ์ ๊ณผ ์ญ์ ๊ฐ ์ผ์ด๋๋ ๊ฒ์ด ์๋๋ผ ์๋ฐฉํฅ์์ ์ฝ์ ๊ณผ ์ญ์ ๊ฐ ์ผ์ด๋๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
์ด๋ฐ ์ด์ ๋๋ฌธ์ ๋ด๊ฐ ์๊ฐํ๋ ์ฝ๋์์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ ๊ฒ ๊ฐ๋ค.
deque ์์ฑ
from collections import deque
dq = deque('love')
print(dq)
# ์ถ๋ ฅ : deque(['l', 'o', 'v', 'e'])
์คํ๊ตฌํ : append(), pop()
dq.append('m') # ์ค๋ฅธ์ชฝ์ ๋ฃ๊ธฐ
print(dq)
# ์ถ๋ ฅ : deque(['l', 'o', 'v', 'e', 'm'])
dq.pop() # ์ค๋ฅธ์ชฝ ์์ ๋ฐํ
print(dq)
# ์ถ๋ ฅ : deque(['l', 'o', 'v', 'e'])
ํ ๊ตฌํ : appendleft(), pop(), append(), popleft()
dq.appendleft('I') # ์ผ์ชฝ์ ๋ฃ๊ธฐ
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e'])
dq.append('y') # ์ค๋ฅธ์ชฝ์ ๋ฃ๊ธฐ
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e', 'y'])
dq.popleft() # ์ผ์ชฝ ์์ ๋ฐํ
print(dq)
# ์ถ๋ ฅ : deque(['l', 'o', 'v', 'e', 'y'])
deque ํ์ฅ : extend(), extendleft()
dq.extend('ou') # ์ค๋ฅธ์ชฝ์ผ๋ก ํ์ฅ
print(dq)
# ์ถ๋ ฅ : deque(['l', 'o', 'v', 'e', 'y', 'o', 'u'])
dq.extendleft('I') # ์ผ์ชฝ์ผ๋ก ํ์ฅ
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e', 'y', 'o', 'u'])
๋ฆฌ์คํธ์ฒ๋ผ ์ฌ์ฉ : insert(), remove()
dq[2] = 'n' # 'o' -> 'n'
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'n', 'v', 'e', 'y', 'o', 'u'])
dq = deque('love')
dq.insert(0, 'I') # 0๋ฒ์งธ ํญ๋ชฉ์ 'I'์ถ๊ฐ
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e'])
dq.insert(100, 'K') # 100๋ฒ์งธ ํญ๋ชฉ(์์ผ๋ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์) 'K'์ถ๊ฐ
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e', 'K'])
dq.remove('K') # 'K'์ญ์ , ๋ง์ฝ ๊ฐ์ ํญ๋ชฉ์ด ์๋ค๋ฉด ์ผ์ชฝ๋ถํฐ ์ญ์ ๋จ.
print(dq)
# ์ถ๋ ฅ : deque(['I', 'l', 'o', 'v', 'e'])
deque ๋ด์ฉ ๋ฐ์ : reverse()
dq.reverse()
print(dq)
# ์ถ๋ ฅ : deque(['e', 'v', 'o', 'l', 'I'])
์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ : https://duckracoon.tistory.com/entry/ํ์ด์ฌ-collections-deque-์ฌ์ฉ๋ฒ๊ณผ-์์ฉ, https://dongdongfather.tistory.com/72
deque์ด์ฉํด์ ๋ค์ ๊ตฌํํ ์ฝ๋
from sys import stdin
from collections import deque
n = int(stdin.readline())
q = deque()
for i in range(1, n + 1):
q.append(i)
while len(q) != 1:
q.popleft()
q.append(q.popleft())
print(q[0])
Uploaded by Notion2Tistory v1.1.0