PAT A1012 The Best Rank (25 分)——多次排序,排名
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 (≤2000), 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
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = ;
struct stu{
int id;
int grade[];
};
vector<stu> vs;
int myrank[maxn][];
char list[] = { 'A', 'C', 'M', 'E' };
int now;
bool cmp(stu s1, stu s2){
return s1.grade[now] > s2.grade[now];
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
for (int i = ; i<n; i++){
int id;
int c, m, e, a;
scanf("%d", &id);
scanf("%d %d %d\n", &c, &m, &e);
stu s;
s.id = id;
s.grade[] = c;
s.grade[] = m;
s.grade[] = e;
a = c + m + e;
s.grade[] = a;
vs.push_back(s);
}
for (now = ; now < ; now++){
sort(vs.begin(), vs.end(), cmp);
myrank[vs[].id][now] = ;
for (int k = ; k < n; k++){
if (vs[k].grade[now] != vs[k - ].grade[now]){
myrank[vs[k].id][now] = k + ;
}
else{
myrank[vs[k].id][now] = myrank[vs[k-].id][now];
}
}
}
for (int i = ; i < m; i++){
int j;
scanf("%d", &j); if (myrank[j][] != ){
int best = maxn, best_k = ;;
for (int k = ; k < ; k++){
if (myrank[j][k]<best){
best_k = k;
best = myrank[j][k];
}
}
printf("%d %c\n", myrank[j][best_k], list[best_k]);
}
else{
printf("N/A\n");
}
}
}
注意点:很简单的一道题目,却花了一个多小时,一开始题目意思理解错了,虽然就算理解对了,也一样。就觉得很麻烦,要开好多个数组,加好多属性,总觉得可能有捷径,就一直不下手。反思一下,就是要用最笨的最麻烦的方法先写出来,如果超时了,再去想哪里可以改进。这道题就是直接开个记录排名的数组,或者在结构体里加上各个成绩的排名属性,每个都排一下序,记录下来。这里唯一的小技巧是记录成绩用一个数组,而不是4个int,这样写cmp函数只要写一个。
PAT A1012 The Best Rank (25 分)——多次排序,排名的更多相关文章
- PAT-1012 The Best Rank (25 分) 查询分数对应排名(包括并列)
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PAT A1146 Topological Order (25 分)——拓扑排序,入度
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- A1012 The Best Rank (25)(25 分)
A1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...
- 1012 The Best Rank (25分) vector与结构体排序
1012 The Best Rank (25分) To evaluate the performance of our first year CS majored students, we con ...
- PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...
- PAT A1122 Hamiltonian Cycle (25 分)——图遍历
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...
- PAT A1142 Maximal Clique (25 分)——图
A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...
- [PAT] 1142 Maximal Clique(25 分)
1142 Maximal Clique(25 分) A clique is a subset of vertices of an undirected graph such that every tw ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
随机推荐
- 转载 基于JAVA每月运势api调用代码实例
代码描述:基于JAVA每月运势api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/58 原文链接:http://outofmemory.cn/code-snip ...
- 【Java深入研究】5、Proxy动态代理机制详解
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- JS模拟实现数组的map方法
昨天使用map方法的时候,突然感觉一直在直接用,也没有试试是怎么实现的,本来想直接搜一篇文章盘一下子,结果没搜到合适的,好吧,那就自己来写一下子吧 今天就来实现一个简单的map方法 首先我们来看一下m ...
- H5新标签(适合新手入门)
H5新标签 文档类型设定 document HTML: sublime 输入 html:4s XHTML: sublime 输入 html:xt HTML5 sublime 输入 html:5 < ...
- 一些关于Viewport与device-width的东西~(转)
内容转自 http://www.cnblogs.com/koukouyifan/p/4066567.html 非常感谢 口口一凡 为我们提供的这篇文章,受益匪浅,特地转到自己的博客收藏起来. 以下是原 ...
- 【转】解决configure: error: C++ compiler cannot create executables问题
转自:http://www.coderbolg.com/content/83.html 啊……天啊,./configure时报错:configure: error: C++ compiler cann ...
- 排错-安装SQl 2008“为SQL Server代理服务提供的凭据无效的解决方法
安装SQl 2008“为SQL Server代理服务提供的凭据无效的解决方法 by:授客 QQ:1033553122 在Windows Server 2008安装SQL Server 2008出现的问 ...
- Java并发编程(八)同步容器
为了方便编写出线程安全的程序,Java里面提供了一些线程安全类和并发工具,比如:同步容器.并发容器.阻塞队列.Synchronizer(比如CountDownLatch) 一.为什么会出现同步容器? ...
- Python 会是我们的未来吗?
Python 热度激增 根据 Stack Overflow 的一项调查显示,Python 不仅在专业领域的使用率得到增长,在普通开发上的使用率也有所提升,有 40% 的受访者表示他们现在正在使用 Py ...
- SpringMVC处理请求
HttpServletBean HttpServletBean主要参与了创建工作,并没有涉及请求的处理. FrameworkServlet FrameworkServlet的service方法里添加了 ...