知识点:1,next_permutation(bedin(),end()=1),用于输出当前序列的下一个序列,入队1 2 3全排列时,当此时序列时2 1 3,则下一次输出2 3 1。
同理,prev_permutation(begin(),end()),输出当前序列的上一序列
这两个函数常用在do-while循环中
// next_permutation(begin(),end()+1) 升序全排列1 2 3,1 3 2,2 1 3等等
// prev_permutation(begin(),end()+1) 降序全排列
//若不存在升序或降序排列,如;1 2 3 没有降序排列,则返回0
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n;
int num[50];
int t = 1,k;
while (~scanf("%d", &k))
{
num[t] = k;
t++;
}
do
{
for (int i = 1; i < t; i++)
printf("%d%c", num[i], i == t-1 ? '\n' : ' ');
} while (prev_permutation(num + 1, num + t));
return 0;
}
本文介绍如何利用C++标准库中的prev_permutation函数来实现序列的全排列,并通过一个示例程序展示了如何生成一个整数序列的所有降序排列。
1万+

被折叠的 条评论
为什么被折叠?



