next_permutation next_permutation:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/han_hhh/article/details/81055320

next_permutation:

stl中提供的计算下一个排列的算法

全排列实现代码

#include<iostream>
#include<algorithm>
using namespace std;
int main(void){
    int a[4]={1,2,3};
    do{
        cout<<a[0]<<a[1]<<a[2]<<endl;

    }while(next_permutation(a,a+3));

    return 0;
}

若为3,2,1.next_permutation的返回值即为false,因为没有下一个排列。但是经过一次函数调用之后会变为1,2,3

全排列的实现过程:(对于任意一个序列,最小的序列是增序,最大的序列是减序)

1.123  从后往前看,首先得到的是最后一位3,单独的一个数不需要交换

2.然后得到23,2小于3,因此下一个序列是132

3.将1和2交换,得到213

4.从后往前,1小于3,将13交换………………


猜你喜欢

转载自blog.csdn.net/han_hhh/article/details/81055320