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. php数组转成php编程代码

    将php数组转成可以在php上面运行的编程代码,支持一维及多维数组 <?php //一维数组 $test1 = array(1,2,3); //二维数组 $test2[0] = array( ' ...

  2. Solr 使用自定义 Query Parser(短语查询,精准查询)

    原文出处:http://blog.chenlb.com/2010/08/solr-use-custom-query-parser.html 由于 Solr 默认的 Query Parser 生成的 Q ...

  3. 创建jdk8基础镜像

    https://blog.csdn.net/qq_35981283/article/details/80738451

  4. Easy前端正确删除datagrid的方式(避免直接删除索引没更新问题)

    在删除传参时,不要传索引来删除行 columns: [[ { title: '代码', field: 'Code', width: 100 }, { title: '名称', field: 'Name ...

  5. web api 请求结果中页面显示的json字符串与json对象结果不一致

    我在前端调用这个api的时候也是百思不得其解,明明看到页面上的结果ID是不一样的,但是在js中使用的时候,却一直有重复ID的情况 后来才发现原来是long这个类型的原因,JavaScript中Numb ...

  6. linq与数据库之添加

    这个是linq的添加显示 代码如下: //添加 private void button2_Click(object sender, EventArgs e) { string strstu = &qu ...

  7. WPF制作歌词动画

    最近再做一个UWP的音乐播放器,今天实现了歌词动画,不是滚动的,滚动的慢慢研究 思路:在右边放了三个textBlock,设置 textBlock的effect属性 <TextBlock.Effe ...

  8. JS DOM对象控制HTML元素详解

    JS DOM对象控制HTML元素详解 方法: getElementsByName()  获取name getElementsByTagName()  获取元素 getAttribute()  获取元素 ...

  9. Asp.net Core过滤器

    Asp.net Core五类过滤器:Authorization Filter.Resource Filter.Acton Filter.Exception Filter.Result Filter.优 ...

  10. Python脱产8期 Day15 2019/4/30

    一 生成器send方法 1.send的工作原理# 1.send发生信息给当前停止的yield# 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 2.例: per ...