PAT A1012 The Best Rank (25分) [排序]

题目

题目链接
已知学生C、M、E三门课程的成绩,需要找到学生在C、M、E和个人平均分A中最好排名

题目分析

1 平均分精度,根据样例计算可知,平均分四舍五入,保留整数部分
2 若某学生多门科目排名相同,则取优先级高的科目,优先级排名为A>C>M>E

解题思路

1 计算出个人平均分
2 按照不同科目排名,不同科目出现同名次取优先级最高

易错点

1 某科目可能多名学生分数相同,必须名次相同,例 90 88 88 88 87 名次为 1 2 2 2 5

代码

Code 01

#include <cstdio>
#include <algorithm>
using namespace std;
struct node {
	int id, best;
	int score[4], rank[4];
} stu[2005];
int exist[1000000], flag = -1;
bool cmp1(node a, node b) {
	return a.score[flag] > b.score[flag];
}
int main() {
	int n, m, id;
	scanf("%d %d", &n, &m);
	for(int i = 0; i < n; i++) {
		scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2],
		      &stu[i].score[3]);
		stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3])
		                  / 3.0 + 0.5;
	}
	for(flag = 0; flag <= 3; flag++) {
		sort(stu, stu + n, cmp1);
		stu[0].rank[flag] = 1;
		for(int i = 1; i < n; i++) {
			stu[i].rank[flag] = i + 1;
			if(stu[i].score[flag] == stu[i-1].score[flag])
				stu[i].rank[flag] = stu[i-1].rank[flag];
		}
	}
	for(int i = 0; i < n; i++) {
		exist[stu[i].id] = i + 1;
		stu[i].best = 0;
		int minn = stu[i].rank[0];
		for(int j = 1; j <= 3; j++) {
			if(stu[i].rank[j] < minn) {
				minn = stu[i].rank[j];
				stu[i].best = j;
			}
		}
	}
	char c[5] = {'A', 'C', 'M', 'E'};
	for(int i = 0; i < m; i++) {
		scanf("%d", &id);
		int temp = exist[id];
		if(temp) {
			int best = stu[temp-1].best;
			printf("%d %c\n", stu[temp-1].rank[best], c[best]);
		} else {
			printf("N/A\n");
		}
	}
	return 0;
}
posted @ 2020-07-22 08:48  JamieHou  阅读(121)  评论(0编辑  收藏  举报