(1,3) (3,1)같은 경우도 모두 출력(중복 순열)을 해주어야 하기 때문에 파이썬 itertools의 product를 사용했다.

 

1번 코드

from itertools import product as p
n,m = map(int,input().split())
num = list(map(int,input().split()))
num.sort()
num = map(str, num)

ans = map(' '.join, p(num, repeat=m))
print('\n'.join(ans))

 

2번 코드

from itertools import product as p
n,m = map(int,input().split())
num = sorted(list(map(int,input().split())))

for i in p(num,repeat = m):
    print(' '.join(map(str,i)))

 

 

결과

 

1번 코드는 for문 사용 없이 어떤 변수에 product의 결과물을 계속 저장하는 방식이다. 변수에 값을 계속 저장해주어야 하므로 메모리를 많이 차지한다. 하지만 반복문이 필요가 없고, sorted 대신 sort를 사용했기 때문에 비교적 빠른 시간 안에 답을 구할 수 있다.

 

2번 코드는 for문을 돌면서 product의 결과값을 문자열로 출력한다. 반복문을 통해 list를 확인해야 하므로 시간이 오래걸리지만, 새로운 변수에 할당을 하는 것이 아니므로 메모리는 적게 사용한다.

 

 


 

 

중복조합 - combination_with_replacement 

중복순열 - product

 

sebinChu