利用递归输出n个数的全排列,以n=3为例,代码如下:
1 #include<stdio.h> 2 const int maxn = 11; 3 int n, P[maxn], hashTable[maxn] = {false}; 4 void generateP(int index) 5 { 6 if(index == n+1) 7 { 8 for(int i = 1; i <= n; i++) 9 { 10 printf("%d", P[i]); 11 } 12 printf("\n"); 13 return; 14 } 15 for(int x = 1; x <= n; x++) 16 { 17 if(hashTable[x] == false) 18 { 19 P[index] = x; 20 hashTable[x] = true; 21 generateP(index + 1); 22 hashTable[x] = false; 23 } 24 } 25 } 26 int main() 27 { 28 n = 3; 29 generateP(1); 30 return 0; 31 }
浙公网安备 33010602011771号