PAT甲级1012 The Best Rank//排名

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.

翻译:为了评估一年级学生的表现,我们只考虑他们三门课程的成绩:C-C程序设计语言、M-数学(微积分或线性代数)和E-英语。同时,我们通过强调学生的最佳排名来鼓励学生,也就是说,在三门课程和平均成绩的四个等级中,我们为每个学生显示出最好的排名。

For example, The grades of C, M, E and A - Average of 4 students are given as the following:
翻译:例如,C、M、E和A—平均4名学生的成绩如下:

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.

翻译:所有学生的最佳排名是第一名,因为第一名的C语言最好,数学第二名,英语第三名,平均最后一名。

Input Specification:

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.

翻译:每个输入文件包含一个测试用例。每个案例以一行包含两个数字N和M(≤2000)的行开始,这两个数字分别是学生总数,以及将分别检查其排名的学生人数。接下来N行,每行包含一个学生ID,是一个由6位数字组成的字符串,后面是该学生的三个整数成绩(范围是[0,100]),顺序是C、M和E。然后是M行,每行包含一个学生ID。

Output Specification:

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.

翻译:对于M个学生中的每一个,在一行中打印出他/她的最佳排名,以及相应排名的符号,用空格隔开。
排序方法的优先级排序为A>C>M>E。因此,如果一个学生有两种或两种以上的方法获得相同的最佳排名,则输出优先级最高的方法。
如果学生不在评分名单上,只需输出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

思路

题目大意给定所有同学的成绩,要求计算对这位同学最好的成绩排名。比如计算机第4,英语第9,数学第2,平均分第5,则最好的排名为数学,第二。如果有名次并列的,则按照A C M E的权重来求。例如计算机第4,英语第3,数学第2,平均分第2,最好的排名算平均分的,是第二名。
这题感觉还是考察排名,比如分数是100 100 100 99 98,则排名分别是1 1 1 4 5。其次要注意如果名次并列要按照权重输出。
解题思路是写一个学生结构体,结构体内分别用两个数组score[]和rank[]存储该学生每门课的成绩和每门课排名。将所有学生全部放入数组中,循环4次,每次都按不同科目的成绩由大到小排序,排完序后排名,再将排名后他的名次放入对应的rank[]中。
举例: 假设score[1]存数学,学生a数学99,排名第一,则a.score[1] = 99,a.rank[1] = 1;学生b数学95,排名第二,则b.score[1] = 95,b.rank[1] = 2。
最后每个学生的rank[]里都存了他所有学科的排名(就像成绩单一样),接着在成绩单里找排名最高的学科即可。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <climits>
using namespace std;
struct stu
{
	int id;
	int rank[4];
	int score[4];
};
int index;
char sub[4] = {'A', 'C', 'M', 'E'};
bool cmp(stu a, stu b) { return a.score[index] > b.score[index]; }
int main()
{
	int n, m;
	cin >> n >> m;
	map<int, bool> mp;
	map<int, stu> hash;
	vector<stu> arr;
	for (int i = 0; i < n; i++)
	{
		stu s;
		//注意成绩存放顺序,是按A>C>M>E的权重顺序存放的
		cin >> s.id >> s.score[1] >> s.score[2] >> s.score[3];
		s.score[0] = (s.score[1] + s.score[2] + s.score[3]) / 3;
		arr.push_back(s);
		mp[s.id] = true;
	}
	//循环4次
	for (index = 0; index < 4; index++)
	{
		//每次先按不同科目成绩排序
		sort(arr.begin(), arr.end(), cmp);
		//开始对该科目排名
		arr[0].rank[index] = 1;
		for (int i = 1; i < n; i++)
		{
			if (arr[i].score[index] == arr[i - 1].score[index])
				arr[i].rank[index] = arr[i - 1].rank[index];
			else
				arr[i].rank[index] = i + 1;
		}
	}
	for (int i = 0; i < arr.size(); i++)
		hash[arr[i].id] = arr[i];
	for (int i = 0; i < m; i++)
	{
		int id;
		cin >> id;
		if (mp[id] != true)
		{
			cout << "N/A" << endl;
			continue;
		}
		//寻找最好的排名
		int best = INT_MAX;
		int best_index = 0;
		for (int j = 0; j < 4; j++)
		{
			if (hash[id].rank[j] < best)
			{
				best = hash[id].rank[j];
				best_index = j;
			}
		}
		cout << best << ' ' << sub[best_index] << endl;
	}
	system("pause");
	return 0;
}

这里解释一下这一段

for (int i = 0; i < n; i++)
{
	stu s;
	//注意成绩存放顺序,是按A-C-M-E的权重顺序存放的
	cin >> s.id >> s.score[1] >> s.score[2] >> s.score[3];
	s.score[0] = (s.score[1] + s.score[2] + s.score[3]) / 3;
	arr.push_back(s);
	mp[s.id] = true;
}

为什么要按照A-C-M-E的权重顺序存放。因为在这段代码里

int best = INT_MAX;
int best_index = 0;
for (int j = 0; j < 4; j++)
{
	if (hash[id].rank[j] < best)
	{
		best = hash[id].rank[j];
		best_index = j;
	}
}

总是只有找到更高的排名时best才会更新,也就是说如果有并列相等的存在,best是不会更新成第二个的。比如排名1 1 2 3,best存储的只会是第一个1而不是第二个。这也正好符合了题目的权重选择要求,A和C相同的情况下,best存储了A的值而不是C的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值