Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define CHECK_ALLOC(p) if (p == NULL) {perror(""); exit(-1);}
- int *readArr(int *n) {
- scanf("%d", n);
- int *arr = (int*)malloc(sizeof(int) * *n);
- CHECK_ALLOC(arr);
- for (int i = 0; i < *n; i++) {
- scanf("%d", arr + i);
- }
- return arr;
- }
- void printArr(int arr[], int size) {
- for (int i = 0; i < size; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- }
- void print_combinations(int arr[], int n, int k, int comb[], int start, int index, int *cnt) {
- if (index == k) {
- printf("#%d: ", ++*cnt);
- printArr(comb, k);
- return;
- }
- for (int i = start; i < n; i++) {
- comb[index] = arr[i];
- print_combinations(arr, n, k, comb, i + 1, index + 1, cnt);
- }
- }
- int main() {
- int n, k;
- int *arr = readArr(&n);
- scanf("%d", &k);
- int *comb = (int*)malloc(sizeof(int) * k);
- if (!comb) {
- free(arr);
- perror("");
- exit(-1);
- }
- int cnt = 0;
- print_combinations(arr, n, k, comb, 0, 0, &cnt);
- free(arr);
- free(comb);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement