The Best Rank

  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 Algrbra), 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 CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 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 Specification:

  Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤), 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 CM and E. Then there are M lines, each containing a student ID.

Output Specification:

  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 Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

解题思路:
  本题给出学生数量n,与查询数量m,之后n行为学生信息,包括id,C语言得分, 数学得分,英语得分,按照平均分、C语言、数学、英语分别排序(项目名分别为A, C, M, E),之后m行查询为学生id,要求输出查询学生的这四个排名中的最高排名,若出现某几项排名相同且都该学生最高排名,输出优先级平均分 > C语言 > 数学 > 英语。

  定义一个结构体student记录每个学生的信息

struct student{
int id; //记录学号
int score[]; //score[0]为平均分 score[1]C语言 score[2]数学 score[3]英语
//为了方便输出直接按优先级顺序记录学生分数
};

  用一个vector容器储存所有学生信息,一个int型的二维数组rank_stu[ i ][ j ]记录排名信息,i为学生排名, j为排名依照项,同样 0 为平均分 1 C语言  2 数学  3 英语 。之后对所有项目进行排名,将得到的排名记录入rank_stu中就可以开始查找了。

  查找时要先判断输入的查询学号存不存在,可以将rank_stu初始化为0,如果查询当前学号某一排名为 0 证明该学号不存在,若存在,比较其所有项目排名并输出最高项排名与项目名。

  AC代码

 #include<bits/stdc++.h>
using namespace std;
int inf = INT_MAX;
//无穷大
const int maxn = ;
//学号最大值
const char course[] = {'A', 'C', 'M', 'E'};
//记录项目名
struct student{
int id; //记录学号
int score[]; //score[0]为平均分 score[1]C语言 score[2]数学 score[3]英语
//为了方便输出直接按优先级顺序记录学生分数
};
vector<student> stu; //记录学生信息
int rank_stu[maxn][]; //记录排名
int courseNow; //当前正在排名的课程
int n, m; //学生数量,查询数量
bool cmp(student a, student b){
return a.score[courseNow] > b.score[courseNow];
}
//排序依照当前courseNow的对应成绩
void getRank(){
memset(rank_stu, , sizeof(rank_stu));
//初始化rank_stu为0
for(int i = ; i < ; i++){ //获得并记录四个项目的排名
courseNow = i; //设定当前排名课程
sort(stu.begin(), stu.end(), cmp); //对stu进行排序
int cnt = ;
vector<student>::iterator preit = stu.begin(); //preit记录获取排名过程中上一个位置迭代器
for(vector<student>::iterator it = stu.begin(); it != stu.end(); it++){
cnt++; //记录当前学生位置
if(it == stu.begin()){ //若it排名第一直接记录排名为cnt(cnt当前值为1)
rank_stu[it->id][courseNow] = cnt;
preit = it; //记录下一个学生的前一个迭代器为it
}else{
if(preit->score[courseNow] == it->score[courseNow]){
//前一个学生与本学生成绩相同则排名相同
rank_stu[it->id][courseNow] = rank_stu[preit->id][courseNow];
}else{
rank_stu[it->id][courseNow] = cnt;
//否则记录当前排名为cnt
}
preit = it;//记录下一个学生的前一个迭代器为it
}
}
}
}
int main(){
while(scanf("%d%d", &n, &m) != EOF){ //输入学生数量与排名数量
while(n--){
student temp;
scanf("%d%d%d%d", &temp.id, &temp.score[], &temp.score[], &temp.score[]);
//输入学生信息
temp.score[] = (temp.score[] + temp.score[] + temp.score[]) / ;
//计算平均分
stu.push_back(temp);
} getRank();
//获取排名
while(m--){
int query;
scanf("%d", &query);
//输入查询id
if(rank_stu[query][] == ){ //学生不存在
printf("N/A\n");
continue;
}
int hRank = inf; //初始化最高排名为无穷大
int hRcourse = -; //hRcourse记录最高排名对应的项目
for(int i = ; i < ; i++){
if(rank_stu[query][i] < hRank){
hRank = rank_stu[query][i];
hRcourse = i;
}
}
printf("%d %c\n", hRank, course[hRcourse]);
//输出排名与项目名
}
}
return ;
}

PTA (Advanced Level) 1012 The Best Rank的更多相关文章

  1. PAT (Advanced Level) 1012. The Best Rank (25)

    简单排序题. 注意:分数相同的人排名相同. #include<iostream> #include<cstring> #include<cmath> #includ ...

  2. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  4. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  5. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  6. PTA(Advanced Level)1075.PAT Judge

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  7. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  8. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  9. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

随机推荐

  1. HttpServletRequest介绍

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息. 二 ...

  2. 由于没有公钥,无法验证下列签名 Ubuntu

    问题:执行 apt-get update 时错误 W: GPG 错误:https://apt.dockerproject.org ubuntu-trusty InRelease: 由于没有公钥,无法验 ...

  3. 利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)

    最近在利用SharePoint Server 2013的列表组织和存储数据,发现SharePoint列表原始不支持级联选择的功能. 谷歌百度一通以后,发现了很多通过代码实现的方案,利用第三方的插件sp ...

  4. 【javascript】点击复制内容的实现

    各种站点有很多类似的代码,不过都是拿来即用,连个解释也没有.大概看了一下,现在主要使用的有两种办法: 1.documen.execCommand("Copy")或者window.c ...

  5. jQuery outerHeight() 方法

    outerHeight() 方法返回第一个匹配元素的外部高度. 如下面的图像所示,该方法包含 padding 和 border. 提示:如需包含 margin,请使用 outerHeight(true ...

  6. sql server 关于表中只增标识问题

    由于我们系统时间用的过长,数据量大,设计是采用自增ID 我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识 没有关闭的时候 ,提示一下错误,不能修改 set identity_inse ...

  7. android 开发 简单的小计算器

    ↑大致效果 项目构成: 随便写的,用的线性布局 activity_main.xml <?xml version="1.0" encoding="utf-8" ...

  8. 虚拟机搭建ftp环境

    引用http://www.cnblogs.com/xiangxiaodong/archive/2013/12/23/3487028.html,学习. 本人是在windows8系统下,Oracle VM ...

  9. 安装zlib的过程(Compression requires the (missing) zlib module)(Python2.6升级为2.7出现的问题)

    觉得有必要把解决问题的过程写下来 1,因为要安装flask,所以安装pip,所以安装setuptools,所以安装zlib.(具体过程http://www.cnblogs.com/aiyr/p/726 ...

  10. 【Qt】Qt在Windows下的开发与调试环境配置

    前文已经交待了从源码编译Qt自定义版本.现在可以开始配置一下开发与调试程序并写个Hello World了. 1. IDE 虽然Qt官方有VS插件使我们可以在VisualStudio中开发Qt应用,但我 ...