1~N ์ค M๊ฐ์ ์๋ฅผ ๊ณจ๋ผ ๋น๋ด๋ฆผ์ฐจ์์ผ๋ก ์ถ๋ ฅํ๋ ๋ฌธ์
(1) ๋ฆฌ์คํธ์ ๋ฏธ๋ฆฌ m๊ฐ๋งํผ์ 0์ ์ ์ฅํ ํ index ์์๋๋ก ์ฑ์๋ฃ์ผ๋ฉฐ ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ
def sequence( seq, n, m, index ) :
1. i๊ฐ seq[index-1] ~ N๊น์ง ๋ฐ๋ณต => ์ด์ ์์น(์ผ์ชฝ)์ ์๋ณด๋ค ํฐ ์๋ง. ๋น๋ด๋ฆผ์ฐจ์์ผ๋ก ์ถ๋ ฅํด์ผํ๊ณ ์ด๋ฏธ ๋์๋ ์์ด์ผ ๊ฒ์ด๊ธฐ ๋๋ฌธ
2. ๋ฆฌ์คํธ seq[index]์ i ๋์
3. index+1 == m์ด๋ฉด seq๋ฅผ ์ถ๋ ฅ
4. ์๋๋ฉด sequence( seq, n, m, index+1 ) ํธ์ถ
# 15650-1.py
def sequence(seq, n, m, index):
for i in range(seq[index-1] if seq[index-1] != 0 else 1, n+1):
seq[index] = i
if index + 1 == m:
print(' '.join(map(str, seq)))
else:
sequence(seq, n, m, index+1)
n, m = map(int, input().split())
nm = [0 for _ in range(m)]
sequence(nm, n, m, 0)
(2) itertools ๋ชจ๋์ combinations_with_replacementํจ์๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
# 15650-2.py
import itertools
n, m = map(int, input().split())
nm = [str(i) for i in range(1, n+1)]
a = list(itertools.combinations_with_replacement(nm, m))
for i in a:
print(' '.join(i))
์๋ถํฐ (2) (1)๋ฒ ์ฝ๋
์์ธ์ง ์ด๋ฒ์ ์๊ฐ์ด ๋น์ทํ๋ค
๋ชจ๋ ํจ์๋ฅผ ๋น์ทํ๊ฒ ์ฝ๋๊ฐ ์ง์ง๊ฑธ๊น ? ใ ใ ใ