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 ...
随机推荐
- .NET 匿名方法的BUG,请专家解答
匿名方法是.NET 3.5之后的一个好东东,很多人使用,但是我在最近的工作当中发现了一个问题. 请专家解答 //list里存放10个数字 List<); ; i < ; i++) { li ...
- Azure静态公网ip自助反解
Linux下安装az工具,并登陆 az login 执行 az network public-ip update --resource-group ip所在资源组名称 --name ip对应资源名称 ...
- angular 响应式表单指令
响应式表单都是以 form开头的指令 第一列指令(不以name结尾)在html模版中,用 [ ] 第二列指令(以name结尾)在html模版中,不用 [ ]
- OpenSSH服务及其相关应用
远程登录工具: telnet,TCP/23:认证明文,数据传输明文,不够安全,所以出现了ssh ssh:Secure SHell,TCP/22,刚开始免费,后来商业化了,所以出现了Openssh,这个 ...
- 巧用网页开发者工具F12 审查、修改元素、去除广告、屏蔽遮罩
巧用网页开发者工具F12 审查.修改元素.去除广告.屏蔽遮罩 每当打开一个网页的时候,是否为页面有很多广告而烦恼:是否为要操作页面(例如观看超清视频),请先注册登录等等事情而麻烦:是否对网页加锁的视频 ...
- ES6学习之ES5之后新增的字符串方法
1.字符串模板:用法:`${变量名}` (好像是C#6.0中也引入了类似的方法.C#中的用法:$"我是{变量名}" ---> $"我叫{name}" ,相 ...
- 使用apache-fileupload处理文件上传与上传多个文件 二(60)
一 使用apache-fileupload处理文件上传 框架:是指将用户经常处理的业务进行一个代码封装.让用户可以方便的调用. 目前文件上传的(框架)组件: Apache----fileupload ...
- 解决:sql server无法在C盘下创建database/操作系统错误5(拒绝访问)
问题: ——无法在C盘的任何位置创建数据库文件 ——在非系统盘的F盘可以创建数据库文件 解决方法1:禁用“以管理员批准模式运行所有管理员" 解决方法2:打开C盘对Users用户的完全控制权限 ...
- ubuntu下apache2使用的简单总结
一. 修改apache2原80端口为90端口 1. 修改/etc/apache2/ports.conf, 将端口80改为90,443,改为444 2. 修改/etc/apache2/sites ...
- Linux之Ubuntu中的安装应用
在Ubuntu中我们经常会使用apt install “APP name” 来安装需要的应用. 从图中我们可以看到,我们使用apt install map命令安装一个map小游戏,这个小游戏是一个用四 ...