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 ...
随机推荐
- phpcms文件结构
主要目录部分 /admin 管理后台目录 -- /skin/ 后台样式 -- /templates/ 后台样式模板 /api api接口 /corpandresiz ...
- 基于FCM的消息推送功能
需求背景 我方项目需要支持客户端消息推送,iOS终端可以借由苹果本身的apns很方便的实现,但是对于Android来说,必须集成第三方的SDK来处理.考虑到项目需要以及成本,我们选择使用谷歌的FCM框 ...
- 网页title旁边的小图片
网页title旁边的小图片设置,图片格式必须是.ico <link rel="icon" href="img/logo.ico" type="i ...
- cf1051F. The Shortest Statement(最短路)
题意 题目链接 题意:给出一张无向图,每次询问两点之间的最短路,满足$m - n <= 20$ $n, m, q \leqslant 10^5$ Sol 非常好的一道题. 首先建出一个dfs树. ...
- POS开发问题 - 跳转页面更新,返回还原旧数据
问题描述:由于需求的需要,路由需要加上缓存 <keep-alive> ,还要实现跳转就初始化,返回就还原的需求.意思就是:页面 A 跳转到页面 B ,页面 B 要初始化数据,但是 页面 B ...
- 前端js优化方案(一)
最近在读<高性能javascript>,在这里记录一下读后的一些感受,顺便加上自己的一些理解,如果有兴趣的话可以关注的我的博客http://www.bloggeng.com/,我会不定期发 ...
- css钻石旋转实现
css钻石旋转实现: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- windows安装ipython
一.安装python2.71.下载地址https://www.python.org/downloads/2.安装后修改本地变量-右击电脑-属性-高级系统设置-环境变量-用户变量-新建-变量名:path ...
- C语言标准库之setjmp
协程的介绍 协程(coroutine),意思就是“协作的例程”(co-operative routines),最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程 ...
- Using an Image for the Layer’s Content
Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...