【PAT甲级 排序】1012 The Best Rank (25 分) C++ 全部AC
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 排序】1012 The Best Rank (25 分) C++ 全部AC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
中規中矩的一道題。排名的順序一開始沒太明白,但是理解之后寫起來挺流暢的,沒什么坑。
解題思路:題目會給出所有學生所有科目的成績、想要看分學生的學號。
- 我們要先給這些學生單科成績排序,算出它們的單科排名,存在Stu中
- 再依次輸出想要看分的學生的最好成績(最好成績的學科 最好成績的排名)。如果學號不存在,就輸出N/A
題解 C++
#include<iostream> #include<string> #include<algorithm> using namespace std; class Stu { public:string id;int C, CC;//C分數 CC排名int M, MM;int E, EE;int A, AA; }; //自定義排序函數:大在前 int cmpC(Stu a, Stu b) {return(a.C > b.C); } int cmpM(Stu a, Stu b) {return(a.M > b.M); } int cmpE(Stu a, Stu b) {return(a.E > b.E); } int cmpTotal(Stu a, Stu b) {return(a.A > b.A); }int main() {int n, m;cin >> n >> m;Stu stu[2000];int count = 0;string id;int C, M, E;for (int i = 0; i < n; i++) {cin >> id >> C >> M >> E;stu[count].id = id;stu[count].C = C;stu[count].M = M;stu[count].E = E;stu[count].A = C + M + E;count++;}string want[2000];for (int i = 0; i < m; i++) {cin >> want[i];}//排序優先級 C->M->E->Totalstable_sort(stu, stu + count, cmpTotal);stu[0].AA = 1;for (int i = 0; i < count; i++) {//存A排名,注意一下并列情況下的排名技巧int curRank = 1;if (stu[i].A == stu[i - 1].A) {stu[i].AA = stu[i - 1].AA;}else {stu[i].AA = curRank = i + 1;}}stable_sort(stu, stu + count, cmpE);stu[0].EE = 1;for (int i = 0; i < count; i++) {//存E排名int curRank = 1;if (stu[i].E == stu[i - 1].E) {stu[i].EE = stu[i - 1].EE;}else {stu[i].EE = curRank = i + 1;}}stable_sort(stu, stu + count, cmpM);stu[0].MM = 1;for (int i = 0; i < count; i++) {//存M排名int curRank = 1;if (stu[i].M == stu[i - 1].M) {stu[i].MM = stu[i - 1].MM;}else {stu[i].MM = curRank = i + 1;}}stable_sort(stu, stu + count, cmpC);stu[0].CC = 1;for (int i = 0; i < count; i++) {//存C排名int curRank = 1;if (stu[i].C == stu[i - 1].C) {stu[i].CC = stu[i - 1].CC;}else {stu[i].CC = curRank = i + 1;}}//輸出科目優先級:A C M Ebool find = false;for (int i = 0; i < m; i++) {find = false;for (int j = 0; j < n; j++) {if (stu[j].id == want[i]) {find = true;if (stu[j].AA <= stu[j].CC && stu[j].AA <= stu[j].MM && stu[j].AA <= stu[j].EE) {cout << stu[j].AA << " A\n";}else if (stu[j].CC <= stu[j].AA && stu[j].CC <= stu[j].MM && stu[j].CC <= stu[j].EE) {cout << stu[j].CC << " C\n";}else if (stu[j].MM <= stu[j].AA && stu[j].MM <= stu[j].CC && stu[j].MM <= stu[j].EE) {cout << stu[j].MM << " M\n";}else if (stu[j].EE <= stu[j].AA && stu[j].EE <= stu[j].CC && stu[j].EE <= stu[j].MM) {cout << stu[j].EE << " E\n";}break;}}if (!find) {cout << "N/A\n";}}system("pause"); }總結
以上是生活随笔為你收集整理的【PAT甲级 排序】1012 The Best Rank (25 分) C++ 全部AC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级 多项式相乘】1009 Pr
- 下一篇: 【PAT甲级 排序】1096 Conse