学习《啊哈算法》
/*
输入一个数n输出1到n的全排列
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int a[10],book[10],n;
void dfs(int step){
//step表示此时站在哪个箱子上
if(step == n+1){
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
return ;
}
//此时站在第step个箱子面前,应该放那张牌呢?
//当然是一一尝试啦!
for(int i =1;i<=n;i++){
//先看看手里还有这张牌没
if(book[i]==0){
a[step]=i;
book[i]=1;
//放好了第step个箱子,该往后走啦
dfs(step+1);
book[i]=0;//尝试完了就收回呗,为了使下次能用
}
}
return ;
}
int main(){
cin>>n;
dfs(1);//首先站在第一个箱子里面
return 0;
}