본문 바로가기

코딩 테스트/백준

[Python] 백준 10974 모든 순열

 

정답 코드

from itertools import permutations

n = int(input())
iter = [i for i in range(1, n+1)]

for i in permutations(iter, len(iter)) :
    ans = i
    print(' '.join(map(str, ans)))

 

N=int(input())

def permutation(n,r,temp=[]):
    if r == 0:
        return print(*temp)
    else:
        for i in range(1,n+1):
            if i not in temp:
                temp.append(i)
                permutation(n,r-1,temp)
                temp.remove(i)

permutation(N,N)