• cb47a_c++_STL_算法_排列组合next_prev_permutation


    cb47a_c++_STL_算法_排列组合next_prev_permutation

    使用前必须先排序。必须是 1,2,3或者3,2,1.否者结果不准确。如果, 1,2,4,6.这样数据不会准确
    next_permutation()//原始数据是从小到大的, 1,2,3
    prev_permutation() //原始数据是从大到小的,比如 3 ,2 ,1,则可以使用这个算法。

    3个数字就6种组合。
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1

    返回值是ture,则还有下一个组合
    false,则没有下一个组合了。

     1 /*cb47a_c++_STL_算法_排列组合next_prev_permutation
     2 
     3 使用前必须先排序。必须是 1,2,3或者3,2,1.否者结果不准确。如果, 1,2,4,6.这样数据不会准确
     4 next_permutation()//原始数据是从小到大的, 1,2,3
     5 prev_permutation() //原始数据是从大到小的,比如 3 ,2 ,1,则可以使用这个算法。
     6 
     7 3个数字就6种组合。
     8 1 2 3
     9 1 3 2
    10 2 1 3
    11 2 3 1
    12 3 1 2 
    13 3 2 1
    14 
    15 返回值是ture,则还有下一个组合
    16 false,则没有下一个组合了。
    17 */
    18 
    19 #include <iostream>
    20 #include <algorithm>
    21 #include <vector>
    22 
    23 
    24 using namespace std;
    25 
    26 template <typename TT8>
    27 void print8(TT8 &ivec)
    28 {
    29     for (TT8::iterator iter=ivec.begin();iter!=ivec.end();++iter)
    30         cout << *iter << ' ';
    31     cout << endl;
    32 }
    33 
    34 int main()
    35 {
    36     vector<int> ivec;
    37     ivec.push_back(1);
    38     ivec.push_back(2);
    39     ivec.push_back(3);
    40 
    41     print8(ivec);
    42 
    43     //next_permutation(ivec.begin(),ivec.end());//1 3 2,第二个组合
    44     //print8(ivec);
    45     //next_permutation(ivec.begin(), ivec.end());//2 1 3 第三个组合
    46     //print8(ivec);
    47     cout << "用循环列出所有的排列组合" << endl;
    48     while (next_permutation(ivec.begin(), ivec.end()))
    49     {
    50         print8(ivec);
    51     }
    52     cout << "---------------" << endl;
    53     vector<int> ivec2;
    54     ivec2.push_back(4);
    55     ivec2.push_back(3);
    56     ivec2.push_back(2);
    57     ivec2.push_back(1);
    58 
    59     print8(ivec2);
    60     cout << "---------------" << endl;
    61     while (prev_permutation(ivec2.begin(), ivec2.end()))
    62     {
    63         print8(ivec2);
    64     }
    65 
    66 
    67 
    68     return 0;
    69 }
  • 相关阅读:
    线性dp 打鼹鼠
    区间dp 能量项链 洛谷p1063
    洛谷 CF1012C Hills (动态规划)
    交作业了 动态规划 木棍加工
    最短路之Floyd
    最小生成树
    寒假集训并查集初级版
    【倍增DP】——保卫王国
    bootstrap四部分概述
    zrender初识
  • 原文地址:https://www.cnblogs.com/txwtech/p/12363928.html
Copyright © 2020-2023  润新知