组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end);
它的作用是得到当前序列之后的全排列,在实际使用的时候,若想要得到一个队列的全排列,需要将该队列按照升序排列。
来看代码:
#include <iostream>
#include <algorithm>
using namespace std;
int s[3] = {1,2,3};
void Cout_Permutation()
{
for(int i = 0;i<3;i++)
cout<<s[i]<<" ";
cout<<endl;
}
int main()
{
do{
Cout_Permutation();
}while(next_permutation(s,s+3));
return 0;
}
///输出结果为
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
倘若将其中3换成2,则得到:
1 2 3
2 1 3
也就是说这个函数只对前n个数进行全排列,n可以由自己决定。