PAT 甲级 A1025

1012 The Best Rank (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 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

输入格式

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.

输出格式

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. 按成绩排序时,注意成绩相同时排名相同,否则测试点3过不去
  2. 存储排名和成绩时,数组的下标0-4依次为A,C,M,E,方便输出
  3. STL真香

AC代码

#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
set<int> s;
int now;
struct student {
	int id;
	int d[4],r[4];  //A,C,M,E4个科目和对应的排名
};
vector<student> stu;
bool cmp(student a, student b) {  //按平均分排
	return a.d[now] > b.d[now];
}
bool cmp5(student a, student b) {  //按平均分排
	return a.id < b.id;
}
int bestGrade(student a) {  //返回最好成绩的下标
	int min = 999999,best;
	for (int i = 0; i < 4; i++)
		if (a.r[i] < min) {
			best = i;
			min = a.r[i];
		}
			
	return best;
}
int main() {
	student temp;
	int n, m, id;
	char subject[] = { 'A','C','M','E' };
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d%d%d%d", &temp.id, &temp.d[1], &temp.d[2], &temp.d[3]);
		temp.d[0] = (temp.d[1] + temp.d[2] + temp.d[3]) / 3;
		stu.push_back(temp);
		s.insert(temp.id);   //插入集合中,方便查找
	}
	for (int i = 0; i < 4; i++) {
		now = i;
		sort(stu.begin(), stu.end(), cmp); //根据now排序
		stu[0].r[i] = 1;
		for (int j = 1; j < stu.size(); j++) {  //计算当前学生每科的排名
			if (stu[j].d[i] == stu[j - 1].d[i]) stu[j].r[i] = stu[j - 1].r[i];
			else stu[j].r[i] = j + 1;
		}
	}

	sort(stu.begin(), stu.end(), cmp5); //按照id排序,方便二分查找
	for (int i = 0; i < m; i++) {
		scanf("%d", &id);
		if (s.find(id) != s.end()) {  //若有此记录
			int left = 0, right = stu.size() - 1;
			int mid = (left + right) / 2;
			while (stu[mid].id != id) {  //二分查找在这里插入代码片
				if (stu[mid].id > id) {
					right = mid - 1;
				}
				else {
					left = mid + 1;
				}
				mid = (left + right) / 2;
			}
			int index = bestGrade(stu[mid]);
			printf("%d %c\n", stu[mid].r[index], subject[index]);
		}
		else {
			printf("N/A\n");
		}
	}
	return 0;
}
发布了16 篇原创文章 · 获赞 0 · 访问量 369

猜你喜欢

转载自blog.csdn.net/qq_38507937/article/details/104068987