[PAT-A 1075]PAT Judge

在这里插入图片描述
在这里插入图片描述
题目大意;
有N位考生,其中准考证号是00001-N。共有K道题,编号为1-k,且每道题的分值给出。
给出M次提交记录,每个提交就显示了该次提交的所属的考生的准考证号,提交题目的题号,所得的分值。
其中分值为-1表示提交了但是未通过编译,如果通过了编译表示则分数为0到该题满分区间的一个整数。
要求对所有考生按照下面的要求进行排序。
1)先按照K道题目的总分从高到低进行排序
2)若总分相同,则按完美解决(满分的题目)的个数由多到少进行排序。
3)如果完美解决的题目的数量也相同,则按照准考证号的大小进行从小到大的排序。
输出规则:
1)输出每位考生的姓名,准考证号,总分,K道题目的各自得分如果总分相等,则排名相同。
2)如果某位考生全场都没有提交记录,或是没有能够通过编译的提交,则该考生的信息不输出。
3)对于需要输出的考生,如果某道题目没有能够通过编译,则该道题目记0分,如果某到题目没有输出记录,则输出’-’。

思路:
1.定义结构体类型Student存放准考证号,每道题的德分,是否有能通过编译的提交(判断该考生是否需要输出),总分,以及该考生的完美解题数。
2.按照题目要求编写cmp函数
3.处理数据。
1)区分“全场都没有提交(不输出)”,“没有能够通过编译的提交”,“有能通过的提交”三种情况。
对考生中的score[]数组初始化为-1,表示没有提交。
2)对每个提交记录:
如果提交了,若分数为-1,表示没有能够通过编译,此时该题对应的分数为0。
如果提交了之后有结果,则score中保存所有结果的最大值。如果为第一次获得满分,则考生的完美解题数+1。
只要有提交,该考生对应的flag=true,表示可以输出。
4.将所有提交记录处理完毕后,算出每个考生的总分,然后进行排序。
5.按照要求进行输出,如果某题的得分为-1,则表示该题该题没有提交,输出’-’。如果该考生的flag=false,则表示该考生没有提交,不输出。

注意:
1.如果一个考生所有能够通过编译的提交都获得了0分,这是考生的总分是0分,但是仍然要输出。

AC代码:

//PAT_A 1075
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10010;
struct Student {
	int id;
	int score[6];
	bool flag;//是否需要输出,只要有提交就输出
	int score_all;//总分
	int solve;//完美解题数
}stu[maxn];
int n, k, m;
int full[6];
bool cmp(Student a, Student b) {
	if (a.score_all != b.score_all)return a.score_all > b.score_all;
	else if (a.solve != b.solve)return a.solve > b.solve;
	else return a.id < b.id;
}
void init() {
	for (int i = 1; i <= n; i++) {
		stu[i].id = i;
		stu[i].score_all = 0;
		stu[i].solve = 0;
		stu[i].flag = false;
		memset(stu[i].score, -1, sizeof(stu[i].score));
	}
}
int main() {
	(void)scanf("%d%d%d", &n, &k, &m);
	init();
	for (int i = 1; i <= k; i++)(void)scanf("%d", &full[i]);//输入每一道题的满分项
	int u_id, p_id, score_obtained;//考生id,题目id,所获分值
	for (int i = 0; i < m; i++) {
		(void)scanf("%d%d%d", &u_id, &p_id, &score_obtained);
		if (score_obtained != -1)stu[u_id].flag = true;//表示有编译通过的
		if (score_obtained == -1 && stu[u_id].score[p_id] == -1)stu[u_id].score[p_id] = 0;//第一次没有编译通过,该题记0分
		if (score_obtained == full[p_id] && stu[u_id].score[p_id] < full[p_id])stu[u_id].solve++;//第一次完美解题
		if (score_obtained > stu[u_id].score[p_id])stu[u_id].score[p_id] = score_obtained;
	}
	for (int i = 1; i <= n; i++) { 
		for (int j = 1; j <= k; j++) {
			if (stu[i].score[j] != -1)stu[i].score_all += stu[i].score[j];
		}
	}
	sort(stu + 1, stu + 1 + n, cmp);
	int r = 1;
	for (int i = 1; i <= n && stu[i].flag == true; i++) {
		if (i > 1 && stu[i].score_all != stu[i - 1].score_all)r = i;
		printf("%d %05d %d", r, stu[i].id, stu[i].score_all);
		for (int j = 1; j <= k; j++) {
			if (stu[i].score[j] == -1)printf(" -");
			else printf(" %d", stu[i].score[j]);
		}
		printf("\n");
	}
	return 0;
}
发布了101 篇原创文章 · 获赞 1 · 访问量 2982

猜你喜欢

转载自blog.csdn.net/weixin_44699689/article/details/104171797