题解 | #小白鼠排队#

小白鼠排队

https://www.nowcoder.com/practice/27fbaa6c7b2e419bbf4de8ba60cf372b

cmp函数:实现自定义sort;sort基于快排,当s1和s2不会发生交换时返回true

struct中定义seq来实现稳定排序

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

struct shu {
    int num;
    string color;
    int seq;
};

bool cmp(shu s1, shu s2) {
    if (s1.num > s2.num) return true;

    if (s1.num == s2.num && s1.seq < s2.seq) return true;

    return false;
}

int main() {

    int n;
    cin >> n;

    vector<shu> vec;
    for (int i = 0; i < n; i++) {
        int num;
        string color;
        cin >> num >> color;
        vec.push_back({num, color, i});
    }

    sort(vec.begin(), vec.end(), cmp);

    for (auto x : vec) {
        cout << x.color << endl;
    }

    return 0;
}

王道考研机试 文章被收录于专栏

包含考研机试打卡表题目

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务