๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€ Python

๋ฐฑ์ค€15649๋ฒˆ ํŒŒ์ด์ฌ N๊ณผM #12-1) ๋ฐฑํŠธ๋ž˜ํ‚น

by Tamii 2020. 10. 19.
๋ฐ˜์‘ํ˜•

๋‚ด ํ’€์ด

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)]

 

๋Œ“๊ธ€