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 ...
随机推荐
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- maven安装,使用说明,及maven Repository如何使用.
maven的使用方法总结一下 1.首先去apache官网下载maven, http://maven.apache.org/download.cgi2.如果是windows系统,选择 apache-ma ...
- 搭建Node.js Redis开发环境
创建项目 初始化为node项目 $npm init 安装redis 安装@types/node, @types/redis, typescript 初始化TypeScript 配置ts ...
- 寻找jar包的方法
在项目开发中经常会遇到资源jar查找难的问题,一种使用maven ,另一种方法是: (1).使用下载地址:https://oss.sonatype.org/content/repositories/r ...
- X64下IIS调用32位的dll
WebAPI项目中遇到了需要调用32位C++的dll的情况,调试的时候能正常调用,但是发布了之后部署在IIS中出现了BadFormatImage异常, 解决方法是在IIS中相应应用程序池=>高级 ...
- A light-weight client-side OAuth library for Java
这个是一个Github上的开源项目-Signpost,主要封装了一些OAuth认证类的方法,项目地址:电极打开 Signpost 什么是Signpost Signpost是一种非常容易.直观的HTTP ...
- 使用create react app教程
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- form自动提交
在做项目中,例如b2b的由商城跳往卖家中心的后台,当他既有会员账号又是卖家的时候,让他登录了商城,就不需要卖家登录的时候,处理过程中可以用到form表单自动登录. 实现简单思路:可以再普通会员登录的地 ...
- Git命令--保存用户名和密码
使用git各项操作时,总是会出现输入密码的弹窗,且需要多次输入,很是繁琐,通过git命令可以记住密码,避免多次操作. 一.创建保存密码的文件 1.在home文件夹,一般是 C:\Documents a ...
- C#高性能Socket服务器IOCP实现
引言我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket类写了一些基于传统异步编程模型的代码(BeginSend.BeginReceive,等等)也看过很多博客的知识,在linux ...