c++全排列
题目描述
按照字典序输出自然数 1 到 n 所有不重复的排列,即 n 的全排列,要求所产生的任一数字序列中不允许出现重复的数字。
输入格式
一个整数 n。
输出格式
由 1∼n 组成的所有不重复的数字序列,每行一个序列。
每个数字保留 5 个场宽。
输入输出样例
输入 #1复制
3
输出 #1复制
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
说明/提示
1≤n≤9。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int>num(n);
int i=0;
for(i=0;i<n;i++){
num[i]=i+1;
}
sort(num.begin(),num.end());
do{
for(int nums:num){
cout<<setw(5)<<nums;
}
cout<<endl;
}while(next_permutation(num.begin(),num.end()));
return 0;
}