当前位置: 代码迷 >> 综合 >> PAT-1012 The Best Rank
  详细解决方案

PAT-1012 The Best Rank

热度:47   发布时间:2023-11-20 00:45:26.0
/*PAT-1012 The Best Rank*/
/*本题考察结构体、排序算法sort的运用,如果用STL库vector可简化计算。*/
/*可能遇到的问题是,排序的分数如果相同,那么他们的排名相同,后面的人排名向后移。
比如: 第一名和第二名的成绩相同,那么他们的排名都是1,而第三名成绩是3.*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>using namespace std;typedef struct{string id;int c;int m;int e;int a;int rank;char subject;
}record;bool sort_a(record re1, record re2)
{return re1.a > re2.a;
}
bool sort_c(record re1, record re2)
{return re1.c > re2.c;
}
bool sort_m(record re1, record re2)
{return re1.m > re2.m;
}
bool sort_e(record re1, record re2)
{return re1.e > re2.e;
}
void print(vector<record>& re){for(int i = 0; i < re.size(); ++i){cout << re[i].id << endl;}
}
void getRank(vector<record>& re, vector<int>& rank, char sub){int val = 1;for(int i = 0; i < re.size(); ++i){if(i == 0){rank[i] = 1;}else{if(sub == 'A' && re[i].a == re[i-1].a){rank[i] = rank[i-1];}else if(sub == 'A' && re[i].a != re[i-1].a){rank[i] = i+1;}else if(sub == 'C' && re[i].c == re[i-1].c){rank[i] = rank[i-1];}else if(sub == 'C' && re[i].c != re[i-1].c){rank[i] = i+1;}else if(sub == 'M' && re[i].m == re[i-1].m){rank[i] = rank[i-1];}else if(sub == 'M' && re[i].m != re[i-1].m){rank[i] = i+1;}else if(sub == 'E' && re[i].e == re[i-1].e){rank[i] = rank[i-1];}else if(sub == 'E' && re[i].e != re[i-1].e){rank[i] = i+1;}}}
}int main()
{vector<record> scores;vector<string> ids;int n,m;cin >> n >> m;vector<int> rank(n);for(int i = 0; i < n; ++i){record score;cin >> score.id >> score.c >> score.m >> score.e;int as = (score.c + score.m + score.e + 1) / 3;score.a = as;scores.push_back(score);}string id;for(int i = 0; i < m; ++i){cin >> id;ids.push_back(id);}//cout << "按平均分排序:" << endl;rank.assign(n,0);sort(scores.begin(), scores.end(), sort_a);getRank(scores, rank, 'A');for(int i = 0; i < n; ++i){scores[i].rank = rank[i];scores[i].subject = 'A';	}//cout << "按C排序:" << endl;//print(scores);rank.assign(n,0);sort(scores.begin(), scores.end(), sort_c);getRank(scores, rank, 'C');for(int i = 0; i < n; ++i){if(rank[i] < scores[i].rank){scores[i].rank = rank[i];scores[i].subject = 'C';	}}//cout << "按数学排序:" << endl;rank.assign(n,0);sort(scores.begin(), scores.end(), sort_m);getRank(scores, rank, 'M');for(int i = 0; i < n; ++i){if(rank[i] < scores[i].rank){scores[i].rank = rank[i];scores[i].subject = 'M';	}}//cout << "按英语排序:" << endl;rank.assign(n,0);sort(scores.begin(), scores.end(), sort_e);getRank(scores, rank, 'M');for(int i = 0; i < n; ++i){if(rank[i] < scores[i].rank){scores[i].rank = rank[i];scores[i].subject = 'E';	}}for(int i = 0; i < m; ++i){int j;for(j = 0; j < n; ++j){if(scores[j].id == ids[i]){cout << scores[j].rank << " " << scores[j].subject << endl;break;}}if(j >= n){cout << "N/A" << endl;}}return 0;
}