A1012 The Best Rank (25)(25 分)
A1012 The Best Rank (25)(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 Algebra), 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
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
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
思考
理解总结之后,在脑子里简化结论,快速应用,才是学习的主要方法啊。
练成反应,李长林啊。

PAT里面排名的思路竟然就和一般人排名思路不一样。
91,90,88,88,84
排名,1、2、3、3、5
而不是正常思维的1、2、3、3、4
AC代码
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
struct Student{
int id;
int grade[4];
}stu[2010];
char course[4] = {'A', 'C', 'M', 'E'};
int Rank[10000000][4] = {0};
int now;
int cmp(const void* a, const void* b){
struct Student*aa=a;
struct Student*bb=b;
return aa->grade[now] <bb->grade[now];//cmp函数中<在c语言对应降序,在c++里对应升序
}
/* 为使qsort达成升序排列
在函数cmp中,如果第一个参数小于第二个参数,它必须返回一个负值;
如果第一个参数等于第二个参数,它必须返回0;
如果第一个参数大于第二个参数,它必须返回一个正值。
降序则反其道而行
*/
int main(){
int n,m;
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] = (stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3]) / 3;
}
for(now = 0; now < 4; now++){
qsort(stu, n,sizeof (struct Student), 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[stu[i].id][now] = Rank[stu[i - 1].id][now]+1;这才是对的吧,排名+1啊
}
}
}
int query;
for(int i = 0; i < m; i++){
scanf("%d", &query);
if(Rank[query][0] == 0){
printf("N/A\n");
}else{
int k = 0;
for(int j = 0; j < 4; j++){
if(Rank[query][j] < Rank[query][k]){
k = j;
}
}
printf("%d %c\n", Rank[query][k], course[k]);
}
}
return 0;
}
给出c++代码,主要是cmp函数的写法不同,因为sort函数与qsort函数要求不同
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student{
int id;
int grade[4];
}stu[2010];
char course[4] = {'A', 'C', 'M', 'E'};
int Rank[10000000][4] = {0};
int now;
bool cmp(Student a, Student b){
return a.grade[now] > b.grade[now];
}
int main(){
int n,m;
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] = (stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3]) / 3;
}
for(now = 0; now < 4; now++){
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;
}
}
}
int query;
for(int i = 0; i < m; i++){
scanf("%d", &query);
if(Rank[query][0] == 0){
printf("N/A\n");
}else{
int k = 0;
for(int j = 0; j < 4; j++){
if(Rank[query][j] < Rank[query][k]){
k = j;
}
}
printf("%d %c\n", Rank[query][k], course[k]);
}
}
return 0;
}
A1012 The Best Rank (25)(25 分)的更多相关文章
- PAT A1012 The Best Rank (25 分)——多次排序,排名
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 1012 The Best Rank (25 分)
1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...
- 怎么设置BarTender中二维码大小为25*25
有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...
- JAVA题目:正整数n若是其平方数的尾部,则称n为同构数 如:5*5=25, 25*25=625 问: 求1~99中的所有同构数
1 /*题目:正整数n若是其平方数的尾部,则称n为同构数 2 如:5*5=25, 25*25=625 3 问: 求1~99中的所有同构数 4 */ 5 //分析:将1-99分为1-9和10-99,用取 ...
- PATA1012The Best Rank(25分)
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- [C++]PAT乙级1010. 一元多项式求导 (25/25)
/* 1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:x^n(n为整数)的一阶导数为n*x^n-1.) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1 ...
- [C++]PAT乙级1005. 继续(3n+1)猜想 (25/25)
/* 1005. 继续(3n+1)猜想 (25) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推 ...
- PAT甲级——A1012 The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- A1012. The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
随机推荐
- HTML <pre> 标签
需求 错落有致的规则说明 ps.我真的是一个后端开发... pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体.
- 字符串实现Base64加密/解密
有时候需要对字符串进行加密,不以明文显示,可以使用此方法,比如对URL的参数加密 using System; using System.Collections.Generic; using Syste ...
- hibernate课程 初探单表映射3-4 组件属性
本节内容: 1 简介组件属性 2 demo 1 简介组件属性: <component name = "address" class = "Address" ...
- 关于echarts和jqeury的结合使用问题
今天在工作上遇到了一个小问题,就是自己在用echarts渲染了一个柱状图,然后点击其他位置,将渲染echart的div进行了清空,这里我所有的echart图形都是在同一个div中渲染的,然后当再次要求 ...
- JS实现正则表达式
一.创建正则表达式 一共有两种方式: 1.直接量:var re = /[0-9]*/; 2.通过RegExp对象的构造函数:var re = RegExp("[0-9]*",&qu ...
- v-if与v-show的区别
一.区别 v-if 动态的向DOM树内添加或者删除DOM元素:“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建:在初始渲染条件为假时,什么也不做. v-sho ...
- 制作X509证书
makecert -r -pe -n "CN=XXX" -b 01/01/2005 -e 01/01/2020 -sky exchange -ss my
- 金庸和古龙,Netweaver和微服务,以及SAP Hybris Revenue Cloud
这周Jerry在长沙客户现场待了几天,感谢易总和彩亮的款待.终于有机会和关注这个公众号的一些CRM顾问们进行线下互动,感觉很不错.得知公众号里某些文章帮助顾问们解决了一些工作中的实际问题,我很高兴.感 ...
- UVA1363 - Joseph's Problem(数学,迷之优化)
题意:给出n和k,1≤n,k≤1e9,计算 切入点是k/i 和 k/(i+1)差距不大.令pi = k/i, ri = k%i.如果pi+1 == pi,那么ri+1 == k - pi(i+1) = ...
- Spark的调度
作业调度简介 设计者将资源进行不同粒度的抽象建模,然后将资源统一放入调度器,通过一定的算法进行调度,最终要达到高吞吐或者低访问延时的目的. Spark在各种运行模式中各个角色实现的功能基本一致,只不过 ...