请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。
输入格式:
输入给出正整数n(<10)。
输出格式:
输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,即序列a1,a2,⋯,an排在序列b1,b2,⋯,bn之前,如果存在k使得a1=b1,⋯,ak=bk 并且 ak+1<bk+1。
输入样例:
3
输出样例:
123 132 213 231 312 321
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
int main (){
int n;
cin>>n;
string s="1";
for(int i=2;i<=n;i++){
s+=i+'0';
}
do{
cout<<s<<endl;
}while(next_permutation(s.begin(),s.end()));
//next_permutation()函数实现按顺序全排列
//对应还有一个prev_permutation()函数
return 0;
}