PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
来源:互联网 发布:微信开发团队 知乎 编辑:程序博客网 时间:2024/05/16 10:09
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A310101 98 85 88 90310102 70 95 88 84310103 82 87 94 88310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
Input
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.
Output
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output "N/A".
Sample Input5 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999Sample Output
1 C1 M1 E1 A3 AN/A
使用STL的sort,自定义比较函数。
注意排名可能并列。
使用map哈希表作最后的查询。
/*2015.7.20cyq*/#include <iostream>#include <vector>#include <algorithm>#include <map>#include <fstream>using namespace std;//ifstream fin("case1.txt");//#define cin finstruct stu{int ID;int C;int M;int E;int A;int bestRank;char bestItem;stu(int id,int c,int m,int e):ID(id),C(c),M(m),E(e){A=(c+m+e)/3;}};bool compA(stu s1,stu s2){return s1.A>s2.A;}bool compC(stu s1,stu s2){return s1.C>s2.C;}bool compM(stu s1,stu s2){return s1.M>s2.M;}bool compE(stu s1,stu s2){return s1.E>s2.E;}int main(){int N,M;cin>>N>>M;if(N==0){int x;while(M--){cin>>x;cout<<"N/A"<<endl;}return 0;}int id,c,m,e;vector<stu> students;for(int i=0;i<N;i++){cin>>id>>c>>m>>e;students.push_back(stu(id,c,m,e));}sort(students.begin(),students.end(),compA);int rankA=1;students[0].bestRank=1;students[0].bestItem='A';for(int i=1;i<N;i++){if(students[i].A<students[i-1].A)rankA=i+1;//没有并列,若并列,rankA不更新students[i].bestRank=rankA;students[i].bestItem='A';}sort(students.begin(),students.end(),compC);int rankC=1;if(students[0].bestRank!=1){students[0].bestRank=1;students[0].bestItem='C';}for(int i=1;i<N;i++){if(students[i].C<students[i-1].C)rankC=i+1;if(rankC<students[i].bestRank){students[i].bestRank=rankC;students[i].bestItem='C';}}sort(students.begin(),students.end(),compM);int rankM=1;if(students[0].bestRank!=1){students[0].bestRank=1;students[0].bestItem='M';}for(int i=1;i<N;i++){if(students[i].M<students[i-1].M)rankM=i+1;if(rankM<students[i].bestRank){students[i].bestRank=rankM;students[i].bestItem='M';}}sort(students.begin(),students.end(),compE);int rankE=1;if(students[0].bestRank!=1){students[0].bestRank=1;students[0].bestItem='E';}for(int i=1;i<N;i++){if(students[i].E<students[i-1].E)rankE=i+1;if(rankE<students[i].bestRank){students[i].bestRank=rankE;students[i].bestItem='E';}}map<int ,int > map;//用于查询for(int i=0;i<N;i++){map[students[i].ID]=i;}int x;while(M--){cin>>x;if(map.find(x)==map.end())cout<<"N/A"<<endl;elsecout<<students[map[x]].bestRank<<" "<<students[map[x]].bestItem<<endl;}return 0;}
- PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
- 【PAT Advanced Level】1012. The Best Rank (25)
- 【c++】PAT (Advanced Level)1012. The Best Rank (25)*
- PAT (Advanced Level) Practise 1012. The Best Rank (25)
- 【PAT】【Advanced Level】1012. The Best Rank (25)
- PAT (Advanced Level) Practise 1012. The Best Rank (25)
- PAT (Advanced) 1012. The Best Rank (25)
- PAT (Advanced) 1012. The Best Rank (25)
- 1012. The Best Rank @ PAT (Advanced Level) Practise
- [PAT (Advanced Level) ]1012. The Best Rank 解题文档
- 浙大 PAT Advanced level 1012. The Best Rank
- 浙大PAT (Advanced Level) Practise 1012 The Best Rank (25)
- PAT (Advanced Level) Practise 1012 The Best Rank (25)
- 1012. The Best Rank (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1075. PAT Judge (25) PAT排名 结构体排序
- PAT(Advanced lecel) 1012. The Best Rank
- Pat(Advanced Level)Practice--1012(The Best Rank)
- PAT (Advanced Level) Practise 1012 The Best Rank
- 2012年5月SAT香港真题解析
- 02 JAVA 对象
- JS中&&和||用法
- C语言中逻辑运算符的陷阱
- Java从网络读取图片并保存至本地
- PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
- No resource found that matches the given name 'android:Widget.Material.ActionButton'
- unity3D的update·函数
- 系统服务-----NotificationManager
- LinearLayout 使用
- C++拷贝构造函数详解
- 嘻唰唰第一批之对象的转换
- 1050. String Subtraction (20)
- Spring MVC笔记