1~N ์ค ์ค๋ณต ์์ด M๊ฐ์ ์๋ฅผ ๊ณจ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ถ๋ ฅํ๋ ๋ฌธ์
(1) ๋ฆฌ์คํธ์ ๋ฏธ๋ฆฌ m๊ฐ๋งํผ์ 0์ ์ ์ฅํ ํ index ์์๋๋ก ์ฑ์๋ฃ์ผ๋ฉฐ ์ถ๋ ฅํ๋ ๋ฐฉ๋ฒ
def sequence( seq, n, m, index ) :
1. i๊ฐ seq[index-1] ~ N๊น์ง ๋ฐ๋ณต => ์ด์ ์์น(์ผ์ชฝ)์ ์๋ณด๋ค ํฐ ์๋ง. ์ค๋ฆ์ฐจ์์ผ๋ก ์ถ๋ ฅํด์ผํ๊ณ ์ด๋ฏธ ๋์๋ ์์ด์ผ ๊ฒ์ด๊ธฐ ๋๋ฌธ
2. ๋ฆฌ์คํธ seq[:index]์ ๋์ ํ๋ ค๋ i ๊ฐ ์๋์ง ํ์ธํ์ฌ ์์ผ๋ฉด continue => ์ค๋ณต ์์ด m๊ฐ์ ์๋ฅผ ๊ณ ๋ฅด๊ธฐ ๋๋ฌธ
3. ๋ฆฌ์คํธ seq[index]์ i ๋์
4. index+1 == m์ด๋ฉด seq๋ฅผ ์ถ๋ ฅ
5. ์๋๋ฉด 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):
if i in seq[:index]:
continue
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ํจ์๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
# 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(nm, m))
for i in a:
print(' '.join(i))
์๋ถํฐ (2) (1) ๋ฒ ์ฝ๋
์ถ๋ ฅํ๋ ์์ด ์๊ฐ ์ค์ด๋๋๊น ์๊ฐ๋ ํ ์ค์๋ค