首页 > 其他 > 详细

next_permutation(,)用法

时间:2014-03-25 14:31:41      阅读:436      评论:0      收藏:0      [点我收藏+]

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


运行:

bubuko.com,布布扣

next_permutation(,)用法,布布扣,bubuko.com

next_permutation(,)用法

原文:http://blog.csdn.net/sr_19930829/article/details/22051981

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!