众所周知有很多情况是求一个序列的所有排列。我们可以用STL里next_premutation函数表示出来比较方便。
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
sort(str.begin(), str.end());
cout << str << endl;
while (next_permutation(str.begin(), str.end()))
{
cout << str << endl;
}
return 0;
}然后还可以用C的int型数组也能进行实现。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int a[100],i,n;
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
//sort(a,a+n);
sort(a,a+n);
do
{
for (i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}while (next_permutation(a,a+n));
return 0;
}

本文介绍如何利用C++ STL中的`next_permutation`函数生成一个序列的所有可能排列,并提供两种实现方式:一种是使用字符串类型,另一种是使用整型数组。这两种方法都能有效地帮助我们解决序列全排列的问题。
4217

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



