zoukankan      html  css  js  c++  java
  • STL算法:next_permutation求全排列

    next_permutation:由原排列得到字典序中下一次最近排列

    int main() {
        int a[] = {1, 2, 3};
        do {
            cout << a[0] << " " << a[1] << " " << a[2] << endl;
        } while (next_permutation(a, a + 3));
        return 0;
    }
    /*1 2 3
      1 3 2
      2 1 3
      2 3 1
      3 1 2
      3 2 1*/

    prev_permutation:由原排列得到字典序中上一次最近排列

    int main() {
        int a[] = {3, 2, 1};
        do {
            cout << a[0] << " " << a[1] << " " << a[2] << endl;
        } while (prev_permutation(a, a + 3));
        return 0;
    }
    /*3 2 1
      3 1 2
      2 3 1
      2 1 3
      1 3 2
      1 2 3*/
  • 相关阅读:
    Activity
    日志
    StringBuffer
    内部类
    接口
    多态
    final关键字
    abstract关键字
    对象初始化
    继承
  • 原文地址:https://www.cnblogs.com/nublity/p/10491303.html
Copyright © 2011-2022 走看看