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 ...
随机推荐
- java反射-使用反射获取类的所有信息
在OOP(面向对象)语言中,最重要的一个概念就是:万事万物皆对象. 在java中,类也是一个对象,是java.lang.Class的实例对象,官网称该对象为类的类类型. Class 类的实例表示正在运 ...
- blueterm蓝牙超级终端(源码)
今天FQ访问外文网站砍看见的终端程序,貌似现在已经开源了,这个软件源码方便开发者开发开发蓝牙与相关的设备的通信,所以在此分享下,给需要的人 项目地址:点击打开,感谢作者,我已经实验过了,导入到ecli ...
- 实战:ADFS3.0单点登录系列-集成MVC
本文将讲解如何让MVC应用程序与ADFS集成,完成认证的过程. 目录: 实战:ADFS3.0单点登录系列-总览 实战:ADFS3.0单点登录系列-前置准备 实战:ADFS3.0单点登录系列-ADFS3 ...
- linux下配置Nginx,支持thinkphp
前言引入 一个刚入行的朋友,刚换工作,入职了一个新公司.新公司一个php开发,就是他.俨然老板把他当成公司扛把子了,把服务器都给了他,让他部署整个php的开发环境.那个朋友是wamp爱好者.然后面对l ...
- webstorm下载激活汉化
下载 官方下载地址:https://www.jetbrains.com/webstorm/ 激活 参考http://blog.csdn.net/it_talk/article/details/5244 ...
- 网络编程——基于UDP的网络化CPU性能检测
网络化计算机性能检测软件的开发,可对指定目标主机的CPU利用率进行远程检测,并自动对远程主机执行性能指标进行周期性检测,最终实现图形化显示检测结果. 网络通信模块:(客户端类似,因为udp是对等通信) ...
- IOS 控制器View的创建方式(方式的优先级 、view的延迟加载)
MJViewController的view的创建 的方式的优先级 控制器view的延迟加载 ● 控制器的view是延迟加载的:用到时再加载 ● 可以用isViewLoaded方法判断一个UIViewC ...
- Redis(5.0.0)持久化AOF和 RDB 结合源码分析
主要是挖个坑.候补(代码还没看完..) https://github.com/antirez/redis/tree/5.0 一.Redis保存持久化文件 二.Redis启动加载持久化文件 src/se ...
- Android(java)学习笔记86:Android提供打开各种文件的API接口:setDataAndType
1. Android 打开各种文件(setDataAndType) private void openFile(File file){ Intent intent = new Intent(); in ...
- 【转】iOS开发4:关闭键盘
在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘.对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭 ...