pat题目链接
我的思路
用map存储数据,然后转存到vector使用自定义sort排序,最后输出。但是过不了最后一个测试点。百思不得其解。
我的代码
#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
int midSore{-1}, finalSore{-1}, testSore{-1}, sore{0};
};
bool comp(const pair<string, Node> &n1, const pair<string, Node> &n2) {
if (n1.second.sore != n2.second.sore) {
return n1.second.sore > n2.second.sore;
} else {
return n1.first < n2.first;
}
}
map<string, Node> Data, Pass;
int main() {
int p, m, n;
cin >> p >> m >> n;
while (p--) {
string s;
int d;
cin >> s >> d;
if (d <= 900)
Data[s].testSore = d;
}
while (m--) {
string s;
int d;
cin >> s >> d;
if (d >= 0 && d <= 100)
Data[s].midSore = d;
}
while (n--) {
string s;
int d;
cin >> s >> d;
if (d >= 0 && d <= 100)
Data[s].finalSore = d;
}
for (auto &x:Data)
if (x.second.testSore >= 200) {
if (x.second.midSore > x.second.finalSore) {
if (x.second.midSore * 0.4 + x.second.finalSore * 0.6 >= 60) {
x.second.sore = lround(x.second.midSore * 0.4 + x.second.finalSore * 0.6);
Pass.emplace(x);
}
} else if (x.second.finalSore >= 60) {
x.second.sore = x.second.finalSore;
Pass.emplace(x);
}
}
vector<pair<string, Node>> vec(Pass.begin(), Pass.end());
sort(vec.begin(), vec.end(), comp);
for (const auto &x:vec)
cout << x.first << " " << x.second.testSore << " " << x.second.midSore << " " <<
x.second.finalSore << " " << x.second.sore << endl;
return 0;
}