题目链接: 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;
}

最新文章

  1. Hibernate 表映射 主键生成策略与复合主键
  2. 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】
  3. Spring注解学习
  4. Java虚拟机6:内存溢出和内存泄露、并行和并发、Minor GC和Full GC、Client模式和Server模式的区别
  5. form表单及其中元素
  6. Quartz.NET开源作业调度框架系列(二):CronTrigger
  7. C#客户端Redis服务器的分布式缓存
  8. ARM寻址方式
  9. Hadoop:使用原生python编写MapReduce
  10. SQL多表查询中的分页,字段组合综合实例解析
  11. Unity 读取Excel
  12. js中使用队列发送ajax请求
  13. csuoj1009
  14. UDP传输包大小(转)
  15. 2013暑假总结-SB学习
  16. .NET开发微信小程序-获取OpenId
  17. C# /VB.NET操作Word批注(一)—— 插入、修改、删除Word批注
  18. day18-网络编程基础(一)
  19. git 线上回滚问题纪要
  20. 腾讯QQ会员中心g_tk32算法【C#版】

热门文章

  1. C#实现在winfrom程序中下载文件
  2. Code repo
  3. logstash 使用grok正则解析日志
  4. JavaEE Tutorials (27) - Java EE的并发工具
  5. codevs2822 爱在心中
  6. 如何让FPGA中的SPI与其他模块互动起来
  7. hdu 2222 Keywords_ac自动机模板
  8. 十个JAVA程序员容易犯的错误
  9. Longtail Hedgehog(DP)
  10. js函数设计原则