给定一个整数 n,将数字 1∼n 排成一排,将会有很多种排列方法。
现在,请你按照字典序将所有的排列方法输出。
输入格式
共一行,包含一个整数 n。
输出格式
按字典序输出所有排列方案,每个方案占一行。
数据范围
1≤n≤7
代码如下:
#include<bits/stdc++.h>
#define LL_int 128
using namespace std;
const int N = 10;
int path[N];
bool st[N];
int n;
void dfs(int u){
if(u==n){
for(int i=0;i<n;i++){
cout<<path[i]<<" ";
}
cout<<endl;
return;
}
for(int i=1;i<=n;i++){
if(!st[i]){
path[u]=i;
st[i]=true;
dfs(u+1);//下一位;
st[i]=false;
}
}
}
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
dfs(0);
return 0;
}