51 lines
1.2 KiB
C
51 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
void dfs(const int n, const int m, int len, int* result, bool* visited, const int* nums);
|
|
int compare(const void *a, const void *b);
|
|
|
|
int main(){
|
|
int N, M;
|
|
scanf("%d %d",&N, &M);
|
|
|
|
int* result = (int*)malloc(sizeof(int)*M);
|
|
int* nums = (int*)malloc(sizeof(int)*N);
|
|
bool* visited = (bool*)malloc(sizeof(bool)*N);
|
|
for(int i=0; i<N; i++) {
|
|
scanf("%d",&nums[i]);
|
|
visited[i] = false;
|
|
}
|
|
qsort(nums,N,sizeof(int), compare);
|
|
dfs(N,M,0,result, visited, nums);
|
|
free(result);
|
|
free(visited);
|
|
free(nums);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void dfs(const int n, const int m, int len, int* result, bool* visited, const int* nums) {
|
|
if(len == m) {
|
|
for(int i=0; i<m; i++){
|
|
printf("%d ",result[i]);
|
|
}
|
|
printf("\n");
|
|
return;
|
|
}
|
|
|
|
for(int next = 0; next<n; next++){
|
|
if(!visited[next]) {
|
|
visited[next] = true;
|
|
result[len++] = nums[next];
|
|
dfs(n,m,len,result,visited, nums);
|
|
len--;
|
|
visited[next] = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
int compare(const void *a, const void *b) {
|
|
return *(int*)a - *(int*)b;
|
|
} |