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

开始技术总结

  • 这一题是个排序题,分为两块首先是结构体的设计,有id记录学号注意是整型不是字符型和再就是一个grade整型数组,记录平均分和其他三门成绩。

  • 在通过额外定义一个整型Rank数组二维,一维记录学号,二维是4个空间记录每一门成绩的排名。

  • 同时定义了一个全局变量now,用于函数cmp,里面的参数使用。这里有一个坑,就是在下文中可以看见,我是额外的通过在for循环定义了j,然后再是now=j,而不是直接的在for循环里面定义now,这里是我一个错误点,导致纠结了好久。应该是for循环里面的变量是不能充当全局变量使用了。

  • 在就是定义一个number数组来存储,后面需要输出排名学生的学号,不能输入一个就输出一个排名。

  • 接下来就是每一位学生每一科排名的处理,重点因为是4科成绩,所以for循环四次,然后每次通过sort函数排序,排好之后每次给第一位学生的该门成绩排名为1,之后,再是给剩下的学生排名,如果成绩相同,那么排名就与之前的学生排名相同,如果不是就排名为i+1,而不是在原来排名的基础上加1。这部分代码如下:

	for (int j = 0; j < 4; j++) {
now = j;
sort(stu, stu + n, cmp);
Rank[stu[0].id][now] = 1;
for (int i = 1; i < n; i++) {
if (stu[i].grade[now] == stu[i - 1].grade[now]) {//如果分数相同则排名相同,与前一位进行比较
Rank[stu[i].id][now] = Rank[stu[i-1].id][now];
}
else {
Rank[stu[i].id][now] = i + 1;
}
} }
  • 然后就是输出部分了,有了之前排好名的Rank数组,如果Rank数组的二维第一位排名为0,说明该学号学生不存在。直接输出N/A,然后如果不为0,那么就需要在4个中寻找排名最靠前的,如果有多个排名相同,按照A>C>M>E的优先级输出一个即可。这里也是为啥在定义结构体Student和数组Rank时,按照0~3,存放的分数和排名顺序是按照这个优先级来的。方便输出。这部分参考代码如下:
	for (int i = 0; i < m; i++) {
if (Rank[number[i]][0] == 0) {
printf("N/A\n");
}
else {
int k = 0;//用于选出Rank[number[i]]中最小的
for (int j = 0; j < 4; j++) {
if (Rank[number[i]][j] < Rank[number[i]][k]) {
k = j;
}
}
printf("%d %c\n", Rank[number[i]][k], course[k]);
} }

所有部分参考代码入下:

//在VS2017上编写,在PAT上通过测试25分满的
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; struct Student {
int id;
int grade[4];
}stu[2010]; char course[4] = { 'A','C','M','E' };
int Rank[1000000][4];//用于记录每位学生的每门成绩排名,且从0到3分别表示A,C,M,E的成绩排名
int now;//记录是哪一门成绩开始比较 bool cmp(Student a, Student b) {
return a.grade[now] > b.grade[now];
} int main() {
int m, n;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &stu[i].id, &stu[i].grade[1], &stu[i].grade[2], &stu[i].grade[3]);
stu[i].grade[0] = round((stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3]) / 3.0);//求出平均分即A
} int number[2010];//用来存储需要输出的学号
for (int i = 0; i < m; i++) {
scanf("%d", &number[i]);
} for (int j = 0; j < 4; j++) {
now = j;
sort(stu, stu + n, cmp);
Rank[stu[0].id][now] = 1;
for (int i = 1; i < n; i++) {
if (stu[i].grade[now] == stu[i - 1].grade[now]) {//如果分数相同则排名相同,与前一位进行比较
Rank[stu[i].id][now] = Rank[stu[i-1].id][now];
}
else {
Rank[stu[i].id][now] = i + 1;
}
} } for (int i = 0; i < m; i++) {
if (Rank[number[i]][0] == 0) {
printf("N/A\n");
}
else {
int k = 0;//用于选出Rank[number[i]]中最小的
for (int j = 0; j < 4; j++) {
if (Rank[number[i]][j] < Rank[number[i]][k]) {
k = j;
}
}
printf("%d %c\n", Rank[number[i]][k], course[k]);
} } system("pause");
return 0;
}

PATA1012The Best Rank(25分)的更多相关文章

  1. 1012 The Best Rank (25分) vector与结构体排序

    1012 The Best Rank (25分)   To evaluate the performance of our first year CS majored students, we con ...

  2. PAT 甲级 1012 The Best Rank (25 分)(结构体排序)

    题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说 ...

  3. PAT-1012 The Best Rank (25 分) 查询分数对应排名(包括并列)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  4. 1012 The Best Rank (25 分)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  5. 【PAT甲级】1012 The Best Rank (25 分)

    题意: 输入两个整数N,M(<=2000),接着分别输入N个学生的ID,C语言成绩,数学成绩和英语成绩. M次询问,每次输入学生ID,如果该ID不存在则输出N/A,存在则输出该学生排名最考前的一 ...

  6. 1012 The Best Rank (25 分)

    1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...

  7. A1012 The Best Rank (25)(25 分)

    A1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...

  8. 1062 Talent and Virtue (25 分)

    1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...

  9. A1075 PAT Judge (25)(25 分)

    A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...

随机推荐

  1. ReentrantLock使用示例

    /** * ReentrantLock是独占锁 * Lock锁的使用,把锁和要用锁同步的代码放在一起,这里就是放在Printer类中了 * 获取到锁后,最后要在finally代码块中手动释放锁 */ ...

  2. SQLServer查看分区表详细信息

    SQL查看分区内记录个数,常规方法需要知道分区函数然后再显示,网上看到一个一句话显示的方法 ), ps.name ) as partition_scheme, p.partition_number, ...

  3. sizeof()计算结构体的大小

    简要说明:结构体成员按照定义时的顺序依次存储在连续的内存空间,但是结构体的大小并不是简单的把所有成员大小相加,而是遵循一定的规则,需要考虑到系统在存储结构体变量时的地址对齐问题. 一.没有成员的结构体 ...

  4. Centos6 No CMAKE_CXX_COMPILER could be found

    [1]问题现象 cmake发生异常:如下图所示 [2]问题分析 Centos6 上使用Cmake出现错误 "No CMAKE_CXX_COMPILER could be found.&quo ...

  5. 解决SpringBoot无法读取js/css静态资源的新方法

    前言 作为依赖使用的SpringBoot工程很容易出现自身静态资源被主工程忽略的情况.但是作为依赖而存在的Controller方法却不会失效,我们知道,Spring MVC对于静态资源的处理也不外乎是 ...

  6. Java并发专栏(一)—— Process vs Thread

    一.前言 程序是代码和数据的集合,是一种静态实体.不具有代码执行和数据处理的能力,更多是一种行为的描述. 如果将程序和处理器结合,处理器将程序加载至内存,然后执行程序代码处理数据.这时就是可执行的程序 ...

  7. Nginx-Tomcat 等运维常用服务的日志分割-logrotate

    目录 一 .Nginx-Tomcat 等常用服务日志分析 Nginx 日志 Tomcat日志 MongoDB 日志 Redis 日志 二 .日志切割服务 logrotate 三.日志切割示例 Ngin ...

  8. C++中的Mat, const Mat, Mat &,Mat &, const Mat &的区别

    Mat, copy传递,不会改变外部变量的Mat. Mat &, reference传递,函数内部修改将会改变外部. const Mat, copy传递,在函数内,不会被修改,也不会影响到外部 ...

  9. flex:1和flex:auto详解

    flex:1和flex:auto详解 首先明确一点是, flex 是 flex-grow.flex-shrink.flex-basis的缩写. flex-grow属性定义项目的放大比例,默认为0,即如 ...

  10. 英语CollaCoriiAsini阿胶CollaCoriiAsini单词

    阿胶(colla Corii Asini)始载于<神农本草经>,是马科动物驴的皮去毛后熬制而成的胶块,其性味甘.平,具有滋阴润肺,补血.止血等功效.主要治疗血虚萎黄,眩晕心悸,肌痿无力,心 ...