PAT-BASIC1080——MOOC期终成绩
我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088
题目描述:
知识点:排序
思路:定义一个结构体scores来保存每个人的3次成绩和总分
一开始用一个map<string, scores>来保存每个人的成绩。为了排序的方便,将能获得合格证书的同学放入一个新的vector<pair<string, scores> >的集合里,用sort函数自定义比较函数来排序。
时间复杂度是O(nlogn),其中n为能获得合格证书的同学的人数。空间复杂度是O(P)。
C++代码:
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
struct scores {
int gP;
int gMidTerm;
int gFinal;
int g;
};
bool compare(pair<string, scores> pair1, pair<string, scores> pair2);
int main() {
int P, M, N;
cin >> P >> M >> N;
map<string, scores> messages;
map<string, scores>::iterator it;
scores tempScores;
string tempName;
int tempScore;
for(int i = 0; i < P; i++) {
cin >> tempName >> tempScore;
if(tempScore >= 200) {
tempScores.gP = tempScore;
tempScores.gMidTerm = -1;
tempScores.gFinal = -1;
messages[tempName] = tempScores;
}
}
for(int i = 0; i < M; i++) {
cin >> tempName >> tempScore;
it = messages.find(tempName);
if(it != messages.end()) {
it->second.gMidTerm = tempScore;
}
}
for(int i = 0; i < N; i++) {
cin >> tempName >> tempScore;
it = messages.find(tempName);
if(it != messages.end()) {
it->second.gFinal = tempScore;
}
}
vector<pair<string, scores> > results;
for(it = messages.begin(); it != messages.end(); it++) {
int gMidTerm = it->second.gMidTerm;
int gFinal = it->second.gFinal;
double g;
if(gMidTerm > gFinal){
g = gMidTerm * 0.4 + gFinal * 0.6;
}else{
g = gFinal;
}
int trueG = (int) g;
if(g - trueG >= 0.5){
trueG++;
}
it->second.g = trueG;
if(trueG >= 60){
results.push_back(make_pair(it->first, it->second));
}
}
sort(results.begin(), results.end(), compare);
for(int i = 0; i < results.size(); i++){
cout << results[i].first << " " << results[i].second.gP << " " << results[i].second.gMidTerm
<< " " << results[i].second.gFinal << " " << results[i].second.g << endl;
}
}
bool compare(pair<string, scores> pair1, pair<string, scores> pair2){
if(pair1.second.g == pair2.second.g){
if(pair1.first.compare(pair2.first) >= 0){
return false;
}else{
return true;
}
}else{
if(pair1.second.g <= pair2.second.g){
return false;
}else{
return true;
}
}
}
C++解题报告: