Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- #define CHECK_ALLOC(p) if (p == NULL) {perror(""); exit(-1);}
- void swap(int *a, int *b) {
- int aux = *a;
- *a = *b;
- *b = aux;
- }
- 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");
- }
- bool ok(int index, int arrang[]) {
- for (int i = 0; i < index; i++) {
- if (arrang[index] == arrang[i]) {
- return false;
- }
- }
- return true;
- }
- void mkAndPrintArrangements(int n, int k, const int arr[], int arrang[], int index, int *cnt) {
- if (index == k) {
- printf("#%d: ", ++*cnt);
- printArr(arrang, k);
- return;
- }
- for (int i = 0; i < n; i++) {
- arrang[index] = arr[i];
- if (ok(index, arrang)) {
- mkAndPrintArrangements(n, k, arr, arrang, index + 1, cnt);
- }
- }
- }
- int main(void) {
- int n, k;
- int *arr = readArr(&n);
- scanf("%d", &k);
- int *arrang = (int*)malloc(sizeof(int) * k);
- if (arrang == NULL) {
- free(arr);
- perror("");
- exit(-1);
- }
- int cnt = 0;
- mkAndPrintArrangements(n, k, arr, arrang, 0, &cnt);
- free(arr);
- free(arrang);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement