코딩 테스트/백준

[백준15655] N과 M (6)

땅어 2020. 3. 14. 15:32
728x90
300x250

문제


https://www.acmicpc.net/problem/15655

 

15655번: N과 M (6)

N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 고른 수열은 오름차순이어야 한다.

www.acmicpc.net

 

생각의 흐름


N과 M (5)와 유사하나 조합이라는 점!

 

 

코드


from itertools import combinations

N,M = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()

for case in combinations(arr, M):
    print(' '.join(map(str, case)))

 

728x90
300x250