Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.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");
- }
- void mkAndPrintPerms(int *arr, int start, int end, int *cnt) {
- if (start == end) {
- printf("#%d: ", ++*cnt);
- printArr(arr, end + 1);
- return;
- }
- for (int i = start; i <= end; i++) {
- swap(&arr[start], &arr[i]);
- mkAndPrintPerms(arr, start + 1, end, cnt);
- swap(&arr[start], &arr[i]);
- }
- }
- int main(void) {
- int n, cnt = 0;
- int *arr = readArr(&n);
- mkAndPrintPerms(arr, 0, n - 1, &cnt);
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement