코딩 테스트/백준
[백준15666] N과 M (12)
땅어
2020. 3. 14. 15:56
728x90
300x250
문제
https://www.acmicpc.net/problem/15666
15666번: N과 M (12)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해야 한다.
www.acmicpc.net
생각의 흐름
N과 M (10)과 유사하지만 중복선택을 허락하는 조합을 구하는 문제
코드
from itertools import combinations_with_replacement
N,M = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
result = []
result = set(result)
for case in combinations_with_replacement(arr, M):
if case not in result:
result.add(case)
print(' '.join(map(str, case)))
728x90
300x250