next_permutation(,)用法

生成n个数的全排列。

比如初始序列为  1 2 3 4  则下一个序列为 1 2 4 3 按序生成。

#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    char a[3]={'a','b','c'};//第一个排列保证正序,有时候根据题目要求,需要对其进行排序处理。
    for(int i=1;i<=6;i++)//i为总共排列的个数  ,及 3!
    {
        for(int j=0;j<3;j++)
            cout<<a[j]<<" ";
        cout<<endl;
        next_permutation(a,a+3);//放在第一个排列的后边,输出第一个排列的下一个排列

    }
    cout<<"*******************"<<endl;
    int b[4]={0,1,2,3};
    for(int i=1;i<=6;i++)
    {
        for(int j=1;j<=3;j++)
            cout<<b[j]<<" ";
        cout<<endl;
        next_permutation(b+1,b+1+3);
    }
    cout<<"*******************"<<endl;
    int c[3]={1,2,3};
    for(int i=1;i<=6;i++)
    {
        for(int j=0;j<3;j++)
        cout<<c[j]<<" ";
        cout<<endl;
        next_permutation(c,c+3);
    }
    return 0;
}


运行:

原文地址:https://www.cnblogs.com/vivider/p/3697522.html