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. SEGGER J-Link install

    Why J-Link? In case you wonder why GNU ARM Eclipse decided to provide support to SEGGER J-Link, the ...

  2. java写入换行符

    写入一个文件,生成文本文档,里面写入1000行字符,但是写出来的没有换行.所以纠结,百度了下,一行完事. String crlf=System.getProperty("line.separ ...

  3. myeclipse过期以后提示过期以后怎么办?!

    昨天电脑上装的myeclipse到期了,不能进到工作空间里边,只有激活和退出选项,在网上百度了一下,有很多破解工具, 1.刚开始直接使用工具破解,没有成功,总是提示要么激活,要么退出 2.继续想办法, ...

  4. PostSharp 结合 log4net 自动记录日志

    环境: VS 2012 PostSharp-4.1.28  (下载地址)https://visualstudiogallery.msdn.microsoft.com/a058d5d3-e654-43f ...

  5. Windows7 64位中出现的KERNELBASE.dll错误的解决方法

    最近在服程序时遇到个问题,电脑是win764位,编译完的exe测试,偶尔总报错,报错是偶尔的,有时候报错很频繁,但是有一次测试,测试了半天都没有报错,我以为好,发布输出没一会儿又报错了,真是崩溃了,所 ...

  6. javacript 实现瀑布流原理和效果, 滚动加载图片【图文解析 附源码】

    先科普下瀑布流吧 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pin ...

  7. c# Net XML文档(2,2)中有错误

    错误如图所示: xml转实体,需求很简单,度娘找了几个方法试了下,转换代码仔细看了看 没毛病啊  但是 就是提示 XML文档(2,2)中有错误,百度搜索了一大会 没解决方案,仔细分析了一下, 最后发现 ...

  8. Impala源码之订阅发布系统的实现

    本文由  网易云发布. 作者:冯宇 本篇文章仅限内部分享,如需转载,请联系网易获取授权. 本文是Impala源码笔记的第一篇,本文主要根据源代码学习一下statestored模块的实现,众所周知,Im ...

  9. iOS View 外层奇怪的黑线

    最近碰到一个问题,当时是为了建了一个能自动适应内容的 Label.根据内容,我计算出需要的尺寸,然后把新尺寸设置给 Label. 但是显示的时候,一个奇怪的现象出现了,Label 的顶端总是有一条浅浅 ...

  10. iOS 访问模拟器数据

    1. 如果文件隐藏,进入命令行,输入: defaults write com.apple.finder AppleShowAllFiles -bool true 2. 进入 Finder,shift ...