PAT-BASIC1080——MOOC期終成績
阿新 • • 發佈:2018-12-18
題目描述:
知識點:排序
思路:定義一個結構體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++解題報告: