样例输入:
3
样例输出:
123
132
213
231
312
321
1 #include <stdio.h> 2 int n; 3 4 void dfs(int step,int* a,int* book) 5 { 6 int i; 7 if(step == n) 8 { 9 for(i = 0;i < n;i++) 10 { 11 printf("%d",a[i]); 12 } 13 printf(" "); 14 return ; 15 } 16 17 for(i = 0;i < n;i++) 18 { 19 if(book[i] == 0) 20 { 21 a[step] = i+1; 22 book[i] = 1; 23 dfs(step+1,a,book); 24 book[i] = 0; 25 } 26 } 27 } 28 int main() 29 { 30 scanf("%d",&n); 31 int a[n+1],book[n+1],i; 32 for(i = 0;i < n;i++) 33 { 34 a[i] = 0; 35 book[i] = 0; 36 } 37 dfs(0,a,book); 38 return 0; 39 }