PTA (Advanced Level) 1012 The Best Rank
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 C, M, E 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 C, M 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的更多相关文章
- PAT (Advanced Level) 1012. The Best Rank (25)
简单排序题. 注意:分数相同的人排名相同. #include<iostream> #include<cstring> #include<cmath> #includ ...
- 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 ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1075.PAT Judge
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- 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 ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
随机推荐
- java随笔——HashMap与红黑树
前言: hashmap是一种很常用的数据结构,其使用方便快捷,接下来笔者将给大家深入解析这个数据结构,让大家能在用的时候知其然,也知其所以然. 一.Map 首先,从最基本的讲起,我们先来认识一下map ...
- Monthly数据类型
Monthly由来 最近在做关于智能财税的项目,大量用到了账期相关的数据操作.项目已有两年历史了,对于账期数据,前辈们用的是DateTime数据类型,即每个月的最后一天就是账期.而用DateTime来 ...
- js实现window.open不被拦截的解决方法汇总
一.问题: 今天在处理页面ajax请求过程中,想实现请求后打开新页面,就想到通过 js window.open 来实现,但是最终都被浏览器拦截了. 二.分析: 在谷歌搜索有没有解决方法,有些说可以通过 ...
- Cookie的创建与删除
Cookie 为 Web 应用程序保存用户相关信息提供了一种有用的方法.例如,当用户访问站点时,可以利用 Cookie 保存用户首选项或其他信息,这样,当用户下次再访问站点时,应用程序就可以检索以前保 ...
- “全栈2019”Java第四章:创建第一个Java程序
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- spring指导的index.html在spring文件夹中的位置
- [Swift]遍历集合类型(数组、集合和字典)
Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...
- mxonline实战8,机构列表分页功能,以及按条件筛选功能
对应github地址:列表分页和按条件筛选 一. 列表分页 1. pip install django-pure-pagination 2. settings.py中 install ...
- mxonline实战3,编写首页及用户登录页面1
对应github地址:首页和用户登陆1 一. 显示首页 1. 修改mxonline/setttings.py 在TEMPLATES代码块修改DIRS为 'DIRS': [os. ...
- 在linux云服务器上运行Jar文件
在linux服务器上运行Jar文件时通常的方法是: $ java -jar test.jar 这种方式特点是ssh窗口关闭时,程序中止运行.或者是运行时没法切出去执行其他任务,有没有办法让Jar在后台 ...