题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

#include <iostream> 
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>

bool compare0(std::pair<int, int>& a, std::pair<int, int>& b) {
    if (a.second != b.second) return a.second > b.second;
    else return a.first < b.first;
}

bool compare1(std::pair<int, int>& a, std::pair<int, int>& b) {
    if (a.second != b.second) return a.second < b.second;
    else return a.first < b.first;
}

void scoresSequence(int way, std::vector<std::string>& names, std::unordered_map<int, int>& scoresmap) {
    std::vector<std::pair<int,int>> scoresvec(scoresmap.begin(), scoresmap.end());
    if (way == 0) {
        std::sort(scoresvec.begin(), scoresvec.end(), compare0);
    }
    else{
        std::sort(scoresvec.begin(), scoresvec.end(), compare1);
    }
    for (auto i: scoresvec) {
        std::cout << names[i.first] << " " << i.second << std::endl;
    }
}

int main() {
    int n;
    std::cin >> n;
    int way;
    std::cin >> way;
    std::vector<std::string> names(n,"");
    std::unordered_map<int, int> scoresmap;
    for (int i = 0; i < n; i++) {
        std::cin >> names[i];
        std::cin >> scoresmap[i];
    }
    scoresSequence(way, names, scoresmap);
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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