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 ...
随机推荐
- JavaEE 7 新特性之WebSocket
开发环境: JDK:1.7及以上 JavaEE:1.7,因为只有javaee7才有websocke的api,也可以使用1.6单都导入websocket-api.jar试试(本人不清楚) 注意:没有使用 ...
- MySQL中有关TIMESTAMP和DATETIME的对比
TIMESTAMP和DATETIME的相同点: 1> 两者都可用来表示YYYY-MM-DD HH:MM:SS[.fraction]类型的日期. TIMESTAMP和DATETIME的不同点: 1 ...
- Redis Windows环境搭建
简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数据类型.内置复制 ...
- .NET导出excel方法
//导出 private string outFileName = ""; private string fullFilename = ""; private ...
- kickstart2019 round_A B. Parcels
思路: 利用了曼哈顿距离和切比雪夫距离之间的转化. 参考: https://blog.csdn.net/Dylan_Frank/article/details/88985444 https://www ...
- 01html基础
01_html 1 Mac中的快捷键 基础快捷键: command+c 复制 command+v 粘贴 command+m 最小化当前窗口 Shift+command+c 桌面环境打开Finder c ...
- hibernate笔记3--hql查询
1.Query:他是一种比较面向对象的查询方式,query查询也叫做hql查询(hibernate query language),使用query查询,需要接受一个 hql语句进行查询 ...
- centos6.2安装内核
http://vault.centos.org/6.2/updates/Source/SPackages/ yum install rpm-build redhat-rpm-config unifde ...
- springMvc-对servletApi的支持以及把后台对象以json方式传到前台
1.对servletApi的支持:request.response以及session.cookie的支持 2.把后台代码以json格式向前台输出: 代码: package com.java.contr ...
- 从用户访问网站流程开始,细说web网络基础
1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...