next_permutation 走一遍

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

标准库的 next_permutation 把序列改成字典序的下一个排列,已经是最后一个时返回 false。运行下面这段程序:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include <algorithm>

int main() {
    vector<int> a = {1, 2, 3};
    int cnt = 0;
    do {
        cnt++;
    } while (next_permutation(a.begin(), a.end()));
    cout << cnt << "/" << a[0] << a[1] << a[2] << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论