728x90
728x90
์์์ ์ฌ๋๋ค์ด ์ ๊ฑฐ๋๋ ์์๋ฅผ (N, K)-์์ธํธ์ค ์์ด์ด๋ผ๊ณ ํ๋ค. ์๋ฅผ ๋ค์ด (7, 3)-์์ธํธ์ค ์์ด์ <3, 6, 2, 7, 5, 1, 4>์ด๋ค.
ํ์ด1
- ๋จ์ํ ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํด์ ํ์๋ค.
- index์ target ๋ณ์๋ฅผ ์ง์ ํด ๋ฆฌ์คํธ์์ target ์ ์ ๊ฑฐํ๊ณ ,
- ๋ค์ ํ์ฌ์์น์์ k๋ฒ์งธ ์ฌ๋์ด ์ ๊ฑฐ๋ ์ ์๋๋ก index ๋ฅผ ๋ณ๊ฒฝ์์ผฐ๋ค.
# BOJ-1158.py
from sys import stdin
input = stdin.readline
n, k = map(int, input().split())
nums = list(range(1, n+1))
result = []
target = k
while(True):
index = nums.index(target)
nums.remove(target)
result.append(target)
if not nums:
break
index += k - 1
if index >= len(nums):
index = index % len(nums)
#print(target, "->", nums[index])
target = nums[index]
print("<{}>".format(", ".join(map(str, result))))
ํ์ด 2
- index ์ target ์ ๊ฐ์ ์๋ฏธ์ด๋ฏ๋ก target ๋ณ์๋ฅผ ์ญ์ ํ๋ค.
- ์ฝ๋๊ฐ ๋์ฑ ๊ฐ๊ฒฐํด์ง!
from sys import stdin
input = stdin.readline
n, k = map(int, input().split())
nums = list(range(1, n+1))
index = k - 1
result = []
while(True):
result.append(nums.pop(index))
if not nums:
break
index += k - 1
if index >= len(nums):
index = index % len(nums)
print("<{}>".format(", ".join(map(str, result))))
ํ์ด3
- ํ์ด2์ ๋ฉ๋ชจ๋ฆฌ, ์๊ฐ์ ๋๊ฐ์
- index ๋๋๊ธฐ ์ฐ์ฐ์ ๊ตณ์ด if ๋ฌธ์ผ๋ก ํ์ธํ์ง ์๊ณ ์ฌ์ฉํด๋ ๋๋ค.
- ์ฐ์ฐ ์์๋ฅผ ๋ฐ๊ฟ์ผํจ
- index ๋๋๊ธฐ ์ฐ์ฐ → pop → index ์ด๋
- ๋ฐ๋ณตํ์๋ n๋งํผ ๋ฐ๋ณต๋๋ค.
- ํ์ด2์์์ While, if not ๋ถ ํ์ํจ.
from sys import stdin
input = stdin.readline
n, k = map(int, input().split())
nums = list(range(1, n+1))
index = k - 1
result = []
for _ in range(n):
index = index % len(nums)
result.append(nums.pop(index))
index += k - 1
print("<{}>".format(", ".join(map(str, result))))
728x90
728x90
'๐ Algorithm > ์๊ณ ๋ฆฌ์ฆ-Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ๋คํธ์ํฌ (DFS) (0) | 2021.03.06 |
---|---|
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ํ๊ฒ ๋๋ฒ (DFS) (0) | 2021.03.06 |
[์๊ณ ๋ฆฌ์ฆ] Day3 - 1967๋ฒ ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2018.08.08 |
[์๊ณ ๋ฆฌ์ฆ] Day3 - 2309๋ฒ ์ผ๊ณฑ ๋์์ด (0) | 2018.08.08 |
[์๊ณ ๋ฆฌ์ฆ] Day3 - 1260๋ฒ DFS์ BFS (0) | 2018.08.08 |