728x90
풀이
"백준 15651, N과 M (3)"와 비슷하다.
1부터 N까지가 아닌 입력받은 수에 대해 모든 조합 가능한 수를 출력하면 된다.
소스코드
#include <stdio.h>
#include <stdlib.h>
int N, M, visited[7], arr[7];
int compare(const void *a, const void *b){
return *(int*)a - *(int*)b;
}
void BT(int depth){
for (int i = 0; i < N; i++){
visited[depth] = arr[i];
if(depth + 1 == M){
for (int idx = 0; idx < M; idx++)
printf("%d ", visited[idx]);
putchar(10);
}else BT(depth + 1);
}
}
int main(){
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++)
scanf("%d", &arr[i]);
qsort(arr, N, sizeof(int), compare);
BT(0);
}
출처 및 참고자료
15656번: N과 M (7)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열
www.acmicpc.net
[백준 15651] N과 M (3) [C]
풀이 모든 조합 가능한 수를 출력하면 된다. 소스코드 #include int N, M, visited[7]; void BT(int depth){ for (int i = 1; i <= N; i++){ visited[depth] = i; if(depth + 1 == M){ for (int idx = 0; idx < M..
kyr-db.tistory.com
728x90
'PS > Baekjoon Online Judge' 카테고리의 다른 글
[백준 15666] N과 M (12) [C] (0) | 2021.08.18 |
---|---|
[백준 15665] N과 M (11) [C] (0) | 2021.08.18 |
[백준 15655] N과 M (6) [C] (0) | 2021.08.18 |
[백준 15651] N과 M (3) [C] (0) | 2021.08.18 |
[백준 15664] N과 M (10) [C] (0) | 2021.08.18 |