๋ฐ์ํ
๋ด ํ์ด
def back (d,n,m):
if d==m:
print(*ans)
return
for i in range(n):
if not visit[i]:
visit[i]=True
ans.append(i+1)
back(d+1,n,m)
ans.pop()
visit[i]=False
n,m=map(int,input().split())
visit=[False]*n
ans=[]
back(0,n,m)
์ ์ ์๋ ๋๋ง ์์๋ณผ ๊ฒ ๊ฐ์ ํ์ด...
๋ค๋ฅธ ํ์ด (๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉํ ๊ฒฝ์ฐ)
from itertools import permutations
n, m = map(int, input().split())
l = list(range(1, n + 1))
res = (list(permutations(l, m)))
for i in range(len(res)):
print(' '.join(map(str, res[i])))
permutations ( ๋ฆฌ์คํธ, ์์ ๊ฐ์)
ex) 2๊ฐ์ ์์ ๊ฐ์ ๋ฌถ์์ผ๋ก ๋๋์ด๋ผ
num=[1,2,3,4]
permutations(num,2)
-> [(1,2), (1,3),(1,4), (2,1), (2,3), (2,4) (3,1), (3,2), (3,4), (4,1), (4,2), (4,3)]
'๐ ์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค15652๋ฒ ํ์ด์ฌ N๊ณผM4 #13-4) ๋ฐฑํธ๋ํน (0) | 2020.10.20 |
---|---|
๋ฐฑ์ค15651๋ฒ ํ์ด์ฌ N๊ณผM 3 #13-3) ๋ฐฑํธ๋ํน (0) | 2020.10.20 |
๋ฐฑ์ค10814๋ฒ ํ์ด์ฌ ๋์ด์์ ๋ ฌ #11-9) ์ ๋ ฌ (0) | 2020.10.18 |
๋ฐฑ์ค1181๋ฒ ํ์ด์ฌ ๋จ์ด์ ๋ ฌ #11-8) ์ ๋ ฌ (0) | 2020.10.17 |
๋ฐฑ์ค11650๋ฒ 11651๋ฒ ํ์ด์ฌ #12์ ๋ ฌ-6)7)์ขํ ์ ๋ ฌํ๊ธฐ (0) | 2020.10.16 |
๋๊ธ