코딩테스트 대비/BOJ
[Baekjoon/Python] 15657번: N과 M (8) - 효과는 굉장했다!
bluetag_boy
2022. 3. 14. 00:18
반응형
15657번: N과 M (8)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
알고리즘 분류
- 백트래킹
SOLUTION
import sys
from itertools import combinations_with_replacement, repeat
N, M = map(int, sys.stdin.readline().split())
num_list = sorted(map(int, sys.stdin.readline().split()))
combi_w_replace = list(combinations_with_replacement(num_list, M)) # combinations_with_replacement()함수 이용
for i in combi_w_replace:
print(*i) # *를 통해 unpacking