输出自然数 11 到 �n 所有不重复的排列,即 �n 的全排列,要求所产生的任一数字序列中不允许出现重复的数字。
输入格式
一个整数 � (1≤�≤9)n (1≤n≤9)。
输出格式
由 1∼�1∼n 组成的所有不重复的数字序列,每行一个序列。每个数字保留 55 个常宽。
Sample 1
Inputcopy | Outputcopy |
---|---|
3 | 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 |
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#define endl '\n'
#define ll long long
#define int ll
using namespace std;
const int N=1e6+7;
int n;
int a[N],vis[N];
void dfs(int x)
{
if(x==n+1)
{
for(int i=1;i<=n;i++) cout<<" "<<a[i];
cout<<endl;
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
a[x]=i;
vis[i]=1;
dfs(x+1);
vis[i]=0;
}
}
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
memset(vis,0,sizeof(vis));
cin>>n;
dfs(1);
return 0;
}