//深度优先算法求数字的全排列
#include <iostream>
using namespace std;
#define N 10
//全部声明为全局变量,为了在递归过程中依然有效
bool u[N]; //u[i]标识数字i是否被使用过
int ans[N]; //ans排列的结果
int n;
void print(){
for (int i=0 ;i<n ;++i) cout<<ans[i]<<" ";
cout<<endl;
}
void dfs(int d){
if (d == n) {
print();
return;
}
for (int i=1 ; i<=n; ++i)
if (!u[i]){
ans[d] = i;
u[i] = 1;
dfs(d+1);
u[i] = 0; //恢复现场
}
}
int main(){
cin>>n;
//Sets buffers to a specified character.
//void *memset( void *dest, int c, size_t count );
memset(u,0,sizeof(u));
dfs(0);
system("pause");
return 0;
}