两个函数的头文件为#include< algorithm>
1.next_permutation
char类型
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
cin>>n;
int a[15];
for(int i=1;i<=n;i++)
a[i]=i;
do{
for(int i=1;i<=n;i++)
cout<<a[i]<<' ';
cout<<endl;
}while(next_permutation(a+1,a+n+1));
return 0;
}
输入3后的结果如下:
string类型
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
int n;
string a="123";
do {
cout <<a<< endl;
} while (next_permutation(a.begin(),a.end()));
return 0;
}
输出结果如下:
2.prev_permutation
char类型
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int a[15];
for (int i = 1; i <= n; i++)
a[i] = n-i+1;
do {
for (int i = 1; i <= n; i++)
cout << a[i] << ' ';
cout << endl;
} while (prev_permutation(a + 1, a + n + 1));
return 0;
}
输入3后的结果如下:
string类型
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
int n;
string a="321";
do {
cout <<a<< endl;
} while (prev_permutation(a.begin(),a.end()));
return 0;
}
输出结果如下:
题目例子https://blog.youkuaiyun.com/weixin_46028214/article/details/115432590