C++ 代码
#include <iostream>
using namespace std;
int n, vis[10], ret[10];
void dfs(int idx){
if(idx == n + 1) {
for(int i = 1; i <= n; ++ i)
printf("%d ", ret[i]);
cout << endl;
return ;
}
for(int i = 1; i <= n; ++ i)
if(!vis[i]){
ret[idx] = i;
vis[i] = 1;
dfs(idx + 1);
vis[i] = 0;
//ret[idx] = 0;
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}