zoukankan      html  css  js  c++  java
  • C++ STL 全排列函数

    C++  全排列函数。。。一听名字就在<algorithm>中。。。

    首先第一个说的是next_permutation:

    #include <algorithm> bool next_permutation( iterator start, iterator end );

    The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

    从说明中可以看到 next_permutation 的返回值是布尔类型。

    注意说明中说的前闭后开区间;

    下面是我打的一段程序说明函数:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <functional>
     5 using namespace std;
     6  
     7 int main()
     8 {
     9     string str;
    10     cin >> str;
    11     long long  ans=1;
    12     sort(str.begin(), str.end(),less<char>());//升序排列
    13     cout << str << endl;
    14     while (next_permutation(str.begin(), str.end()))
    15     {
    16         cout << str << endl;
    17         ans++;
    18     }
    19     cout<<"the next string:"<<str<<endl;
    20     cout<<"total forms:"<<ans<<endl;
    21     return 0;
    22 }
    23 //在STL中,除了next_permutation外,还有一个函数prev_permutation,
    24 //两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者
    25 //是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:
    26 //对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,
    27 //c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},
    28 //同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},
    29 //其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
    30 /*
    31 用next_permutation和prev_permutation求排列组合很方便,
    32 但是要记得包含头文件#include <algorithm>。
    33 虽然最后一个排列没有下一个排列,用next_permutation会返回false,
    34 但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。
    35 */
    代码+说明

    2.prev_permutation

    上一个代码内讲到了它

    这就不细说了:

     1  1 //prev_permutation
     2  2 
     3  3 #include <iostream>
     4  4 #include <algorithm>
     5  5 #include <string>
     6  6 #include <functional>
     7  7 using namespace std;
     8  8  
     9  9 int main()
    10 10 {
    11 11     string str;
    12 12     cin >> str;
    13 13     long long  ans=1;
    14 14     sort(str.begin(), str.end(),greater<char>());//降序排列
    15 15     cout << str << endl;
    16 16     while (prev_permutation(str.begin(), str.end()))
    17 17     {
    18 18         cout << str << endl;
    19 19         ans++;
    20 20     }
    21 21     cout<<"the next string:"<<str<<endl;
    22 22     cout<<"total forms:"<<ans<<endl;
    23 23     return 0;
    24 24 }
    25 25 //在STL中,除了prev_permutation外,还有一个函数next_permutation,
    26 26 //两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者
    27 27 //是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:
    28 28 //对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,
    29 29 //c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},
    30 30 //同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},
    31 31 //其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
    32 32 /*
    33 33 用next_permutation和prev_permutation求排列组合很方便,
    34 34 但是要记得包含头文件#include <algorithm>。
    35 35 虽然第一个排列没有上一个排列,用prev_permutation会返回false,
    36 36 但是使用了这个方法后,序列会变成字典序列的最后一个,如abc变成cba。next_permutation同理。
    37 37 */
    38 程序+说明
    直接上代码


     

    部分转载自:http://leonard1853.iteye.com/blog/1450085

    以上完毕。

     

     

    欢迎大家吐槽或评论,如要转载请注明地址,谢谢~(虽然不一定有人能看到。。。)
  • 相关阅读:
    Luckysheet如何初始化含合并单元格的数据
    Luckysheet如何一键导入本地Excel
    又发现一款纯js开源电子表格Luckysheet
    Sublime Text3 注册码(Windows/Build 3176版本)| 开发工具
    Python初学者笔记(4)-简单的通讯录
    Python初学者笔记(3):输出列表中的奇数/奇数项,字符串中的偶数项,字符串大小写转换
    安装wampserver出现“The Program can't start because MSVCR110.dll is missing from your computer. Try reinstalling the program to fix this problem”
    python初学者笔记(2):阿拉伯数字转换成中文大写
    Python初学者笔记:打印出斐波那契数列的前10项
    JavaScript弹出对话框的三种方式
  • 原文地址:https://www.cnblogs.com/Slager-Z/p/7192508.html
Copyright © 2011-2022 走看看