用vis[]数组记录数字是否用过了
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<cstdlib>
#include <algorithm>
#include<string.h>
#include<math.h>
#define llu unsigned long long
using namespace std;
int n,a[10],vis[10]={
0};
void dfs(int i)
{
if(i>n)
{
for(int j=1;j<=n;j++)
{
printf("%5d",a[j]);
}
cout << endl ;
return ;
}
for(int j=1;j<=n;j++)
{
if(vis[j]==0)
{
a[i]=j;
vis[j]=1;
dfs(i+1);
vis[j]=0;
}
}
return ;
}
int main()
{
cin >> n ;
dfs(1);
return 0;
}