728x90
https://www.acmicpc.net/problem/15655
15655번: N과 M (6)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
아이디어
중복이 없는 순열을 뽑아야함으로 콤비네이션처럼 s의 인자를 i + 1로 두어 상태트리를 생성해나간다.
def DFS(L, s):
if L == m:
for i in range(m):
print(res[i], end=" ")
print()
else:
for i in range(s, n):
res[L] = arr[i]
DFS(L+1, i+1)
n, m = map(int, input().split())
arr = list(map(int, input().split()))
res = [0] * m
arr.sort()
DFS(0, 0)
완성
728x90
'알고리즘' 카테고리의 다른 글
| [백준] 15657번: N과 M (8) - python (0) | 2024.04.08 |
|---|---|
| [백준] 15656번: N과 M (7) - python (0) | 2024.04.08 |
| [백준] 15654번: N과 M (5) - python (0) | 2024.04.08 |
| [백준] 15650번: N과 M (2) - python (0) | 2024.04.08 |
| [백준] 15652번: N과 M (4) - python (0) | 2024.04.08 |