알고리즘/백준 문제풀이

[boj] 백준 15657 N과 M (8) python 풀이

감자156 2023. 8. 10. 04:51
반응형

문제

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

 

15657번: N과 M (8)

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

www.acmicpc.net

 

문제 풀이

백트래킹

 

코드

import sys
input = sys.stdin.readline

N, M = map(int,input().split())
LIST = sorted(list(map(int,input().split())))

total = []
def back(s):
    if len(total) == M:
        print(*total)
        return total
    
    for i in range(s,len(LIST)):
        total.append(LIST[i])
        back(i)
        total.pop()


back(0)
반응형