输出自然数 11 到 �n 所有不重复的排列,即 �n 的全排列,要求所产生的任一数字序列中不允许出现重复的数字。
输入格式
一个整数 � (1≤�≤9)n (1≤n≤9)。
输出格式
由 1∼�1∼n 组成的所有不重复的数字序列,每行一个序列。每个数字保留 55 个常宽。
#include<bits/stdc++.h>
typedef long long ll;
#define endl '\n'
#define buff ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
using namespace std;
int n,vis[12],a[12];
void dfs(int x)
{
if(x==n+1)
{
for(int i=1;i<=n;i++) cout<<" "<<a[i];
cout<<'\n';
return ;
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
a[x]=i;
vis[i]=1;
dfs(x+1);
vis[i]=0;
}
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
cin>>n;
dfs(1);
}