【PAT】1012. The Best Rank (25)
题目链接: http://pat.zju.edu.cn/contests/pat-a-practise/1012
题目描述:
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 Algrbra), 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
分析:
(1)考察对结构体的排序。
(2)注意处理成绩相同的情况,如 1 1 3 4, 因为有两个人并列第一,所以第二名实际是排在第三的位置。
代码来自于:http://blog.csdn.net/sunbaigui/article/details/8656832#reply
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
int n;//student
int m;//query
typedef struct Node
{
string name;
int grade[4];
int bestRank;
int bestGrade;
}Node;
typedef struct SortNode
{
int g, idx, rank;
bool operator>(const SortNode& orh)const
{
return g>orh.g;
}
}SortNode;
char CharTable[4]={'A','C','M','E'};
#define INF 0x6FFFFFFF
int main()
{
//input
scanf("%d%d",&n,&m);
vector<Node> stu(n);//student
map<string, int> stuMap;//for query
for(int i = 0; i < n; ++i)
{
cin>>stu[i].name;
scanf("%d%d%d", &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];
stu[i].bestRank = INF;
stuMap[stu[i].name] = i;
}
//
for(int i = 0; i < 4; ++i)//for every record
{
vector<SortNode> sortNode(n);
for(int j = 0; j < n; ++j)
{
sortNode[j].g = stu[j].grade[i];
sortNode[j].idx = j;
}
sort(sortNode.begin(), sortNode.end(), greater<SortNode>());
//process the same grade case
int nowGrade = INF;
int nowRank = 0;
for(int j = 0; j < n; ++j)
{
if(sortNode[j].g == nowGrade)
sortNode[j].rank = nowRank;
else
{
sortNode[j].rank = j;
nowRank = j;
nowGrade = sortNode[j].g;
}
}
//then compare to select the best rank
for(int j = 0; j < n; ++j)
{
int idx = sortNode[j].idx;
int rank = sortNode[j].rank;
if(stu[idx].bestRank > rank)
stu[idx].bestRank = rank, stu[idx].bestGrade = i;
}
} //query
for(int i = 0; i < m; ++i)
{
string name;
cin>>name;
map<string, int>::iterator it = stuMap.find(name);
if(it!=stuMap.end())
{
int idx = it->second;
printf("%d %c\n", stu[idx].bestRank+1, CharTable[stu[idx].bestGrade]);
}
else printf("N/A\n");
}
return 0;
}
【PAT】1012. The Best Rank (25)的更多相关文章
- PAT甲 1012. The Best Rank (25) 2016-09-09 23:09 28人阅读 评论(0) 收藏
1012. The Best Rank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...
- 【PAT甲级】1012 The Best Rank (25 分)
题意: 输入两个整数N,M(<=2000),接着分别输入N个学生的ID,C语言成绩,数学成绩和英语成绩. M次询问,每次输入学生ID,如果该ID不存在则输出N/A,存在则输出该学生排名最考前的一 ...
- 【PAT】1010. 一元多项式求导 (25)
1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数 ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- 【PAT】B1070 结绳(25 分)
此题太给其他25分的题丢人了,只值15分 注意要求最终结果最长,而且向下取整 #include<stdio.h> #include<algorithm> using names ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- 【PAT】1060 Are They Equal (25)(25 分)
1060 Are They Equal (25)(25 分) If a machine can save only 3 significant digits, the float numbers 12 ...
- 【PAT】1012. 数字分类 (20)
1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算 ...
- PAT 甲级 1012 The Best Rank (25 分)(结构体排序)
题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说 ...
随机推荐
- Oracle EBS-SQL (CST-2):检查有BOM但成本不基于累积的数据.sql
select c.segment1 物料编码, ...
- java 编码 UTF-8、ISO-8859-1、GBK 【转】
Java支持UTF-8.ISO-8859-1.GBK等各种字体编码,可笔者发现Java中字体编码的问题仍难倒了不少程序员,网上虽然也有不少关于在Java中如何正确显示中文的文章,但都不够全面,笔者特意 ...
- git 拉取远程分之到本地
git checkout -b newbranch_name --track origin/feature/newbranch_name 如果遇到类似: fatal: git checkout: up ...
- spark快速入门之最简配置 spark 1.5.2 hadoop 2.7 配置
配置的伪分布式,ubuntu14.04上 先配置hadoop,参见这个博客,讲的很好 http://www.powerxing.com/install-hadoop/, 但是我在配的过程中还是遇到了问 ...
- [转载]CTO和技术总监区别
原文地址:http://blog.sina.com.cn/s/blog_6024cfa90101cb0h.html 技术总监(Chief Technical Officer)与CTO(Chief Te ...
- Java面试题之九
四十六.Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 对于这个题,只要弄清楚Math提供的三个与取整相关的方法就OK了. 1.ceil,英文含义是天花板,该 ...
- [置顶] viewPager控制滑动速度和时间
遇到如下问题:viewpager滑动时如果想跳过很多条直接到最后一条,中间会黑屏.黑屏是因为中间的view没有加载出来的缘故. stackOverflow上看到的解决方案,在这里记录一下, publi ...
- 常见Web Service 使用网址
下面的网址是常用Web Service http://www.webxml.com.cn/zh_cn/web_services.aspx 里面的很多Web Service接口...可以用到的,很有用!
- JavaScript阻止事件冒泡
今天在自学敲代码的时候发现了一个问题,当时的例子如下: <!DOCTYPE html> <html lang="en"> <head> < ...
- windows server 2008 应用程序池自动关闭 C:\Windows\system32\RpcProxy\RpcProxy.dll failed to load
Sign In Join Search IIS Home Downloads Learn Reference Solutions Blogs Forums HomeIIS.NET Forums64-b ...