-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutation.c
44 lines (39 loc) · 916 Bytes
/
Permutation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<stdlib.h>
#define NUM 10
void printans(int *A, int *B, int n, int m);
int main(){
int n, k = 0;
int Input[NUM];
while (scanf("%d", &n) != EOF && n != 0){
Input[k] = n;
k++;
}
for (int i = 0; i < k; i++){
int t = Input[i];
int *A = (int *)malloc(t * sizeof(int));
int *B = (int *)malloc(t * sizeof(int));
for(int j= 0; j < t; j++) B[j] = 0;
printans(A, B, 1, t);
}
return 0;
}
void printans(int *A, int *B, int n, int m){
if (n > m){
for (int i = 0; i < m - 1; i++){
printf("%d ", A[i]);
}
printf("%d\n", A[m - 1]);
return;
}
for (int i = 0; i < m; i++){
if (B[i] != 1){
A[n - 1] = i + 1;
B[i] = 1;
n++;
printans(A, B, n, m);
n--;
B[i] = 0;
}
}
}