用递归求出n的全排列


1
include<cstdio> 2 const int maxn = 11; 3 int n,p[maxn], hashTable[maxn] = { false };//hashtable[]为一个整型数组,但可以与真和非相互转换 4 void generatep(int index) { 5 if (index == n + 1) //递归基 6 { 7 for (int i = 1; i <= n; i++) { 8 printf("%d", p[i]); 9 } 10 printf("\n"); 11 return; 12 } 13 for (int x = 1; x <= n; x++) { 14 if (hashTable[x]== false) { 15 p[index] = x; 16 hashTable[x] = true; 17 generatep(index + 1);//你完全可以相信递归可以做到。 18 hashTable[x] =false ; 19 } 20 } 21 } 22 int main() 23 { 24 int index = 1; 25 scanf("%d", &n); 26 generatep(index); 27 }

 

posted @ 2021-04-17 17:31  银发制御  阅读(294)  评论(0)    收藏  举报