多关键字排完是什么顺序

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

六个任务 A~F 各有截止时刻和收益,先按截止时刻从早到晚、截止相同再按收益从高到低排。

运行下面这段程序:

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

struct Job {
    char name;
    int deadline, profit;
};

int main() {
    vector<Job> jobs = {{'A', 2, 50}, {'B', 1, 30}, {'C', 2, 80}, {'D', 1, 60}, {'E', 3, 20}, {'F', 3, 70}};
    sort(jobs.begin(), jobs.end(), [](const Job& a, const Job& b) {
        if (a.deadline != b.deadline) return a.deadline < b.deadline;
        return a.profit > b.profit;
    });
    for (const Job& j : jobs) cout << j.name;
    cout << endl;
}

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

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

                        
👩‍🏫
AI
💬 题目评论
🔒 登录并作答后可看讨论