TB椰程 TypeBuddy 打字搭子

next_permutation 全排列

CSP-J · 编程模板 · 片段 · cpp · 难度 3/5 · 共 420 字

生成下一字典序排列

  • STL
  • 全排列

前置内容

正文

// next_permutation:生成下一字典序排列
// 需 <algorithm>,序列要先升序
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
vector<int> a = {1, 2, 3};
// 原地改成下一排列;无更大返回 false
bool nx = next_permutation(
    a.begin(), a.end());
// 配合 do-while 可遍历全排列
do {
    // 此时 a 是一种排列,可处理
    cout << a[0] << a[1] << a[2];
} while (next_permutation(
    a.begin(), a.end()));
// 起始必须排好序,否则会漏排列
// prev_permutation 则是求上一排列

CSP-J · 编程模板的其它内容

打字首页 · 词库画廊 · 编程打字 · 指法入门 · 天梯榜 · 数据分析 · 班级课堂 · 关于我们
椰程 TypeBuddy 打字搭子 —— 键盘指法练习 · 单词记忆 · 班级课堂 · 在线 PK