华为机试:统计射击比赛成绩

题目来源

  • 华为机试:统计射击比赛成绩

题目描述

华为机试:统计射击比赛成绩_第1张图片

题目解析

#include 
#include 
#include 
#include 
#include 
using namespace std;

struct Info{
    int id;
    int score;
    Info() : id(0), score(0){
    }
    Info(int id, int score) : id(id), score(score){
    }
};

std::vector<int> split(std::string str, char ch){
    std::vector<int> ans;
    str += ch;
    int j = 0;
    for (int i = 0; i < str.size(); ++i) {
        if(str[i] == ch){
            ans.push_back(stoi(str.substr(j, i - j)));
            j = i + 1;
        }
    }
    return ans;
}

int main() {
    int N;
    std::string str1, str2;
//    std::cin >> N;
//    std::cin >> str1;
//    std::cin >> str2;
    std::vector<int> ids = split("3,3,7,4,4,4,4,7,7,3,5,5,5", ',');
    std::vector<int> scores = split("53,80,68,24,39,76,66,16,100,55,53,80,55", ',');
    std::map<int, std::priority_queue<int>> map;
    for (int i = 0; i < N; ++i) {
        map[ids[i]].push(scores[i]);
    }
    int score;
    std::vector<Info> ans;
    for(auto &it : map){
        if(it.second.size() < 3){
            continue;
        }

        int id = it.first;
        int scoreSum = 0;
        for (int i = 0; i < 3; ++i) {
            scoreSum += it.second.top(); it.second.pop();
        }
        ans.emplace_back(id, scoreSum);
    }
    std::sort(ans.begin(), ans.end(), [](Info &a, Info &b){
        return a.score > b.score ? true : (a.score == b.score ? a.id > b.id : false) ;
    });
    for (auto & an : ans) {
        std::cout << an.id  <<"\t";
    }
}

你可能感兴趣的:(算法与数据结构,华为,c++,开发语言)