遞迴_排列所有組合_我還是不會

 avatar
user_3763047219
c_cpp
2 years ago
893 B
0
Indexable
Never
#include <stdio.h> 
void permute(int* a, int l, int r, int n);



void permute(int* a, int l, int r, int n) {
    if (l == n) {
        for (int i = 1; i <= n; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    else {
        for (int j=l; j <= n; j++) {
            int temp1 = a[l];
            a[l] = a[j];
            a[j] = temp1;
            permute(a, l + 1,r, n);
            int temp2 = a[l];
            a[l] = a[j];
            a[j] = temp2;
        }
    }
}

#include <stdio.h> 
#include <stdlib.h> 


void swap(int* x, int* y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int n, i;
    scanf("%d", &n);
    int* a = (int*)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    permute(a, 0, n - 1, n);
    free(a);
    return 0;
}