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;
}


运行:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值