[PAT-A 1062]Talent and Virtue

在这里插入图片描述
在这里插入图片描述
题目大意:
给出n个考生的准考证号,德分,才分,以及及格线L,优秀线H,然后对这n个考生进行分类。
1.如果德分与才分有一个低于L,则不及格,则为第5类。设下面4类均及格
2.如果德分与才分均不低于H,则为第1类
3.如果德分低不于H,才分低于H,则为第二类
4.如果德分与才分均低于H,但德分>=才分,则为第三类。

1.先按类别从小到大排序。
2.类别相同,按总分从小到大排序。
3.总分相同,按德分从小到大排序。
4.德分相同,按准考证号从小到大排序。

思路:
1.定义结构体,用以存储考生的准考证号,德分,才分,总分以及类别。
2.使用sort函数进行排序,cmp函数按照提议中的规则书写。

AC代码:

//PAT_A 1062 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Students {
	char id[10];
	int moral, talent, sum;//德分,才分,总分
	int flag;//表示第几类考生
}stu[100010];
bool cmp(Students a, Students b) {
	if (a.flag != b.flag)return a.flag < b.flag;//类别从小到大排序
	else if (a.sum != b.sum)return a.sum > b.sum;//同一类别,总分高的在前
	else if (a.moral != b.moral)return a.moral > b.moral;//总分相同,德分高的在前
	else return strcmp(a.id, b.id) < 0;//德分相同,准考证号小的在前
}
int main() {
	int n, L, H, m = 0;//m表示不及格人数
	(void)scanf("%d %d %d", &n, &L, &H);
	for (int i = 0; i < n; i++) {
		(void)scanf("%s %d %d", stu[i].id, &stu[i].moral, &stu[i].talent);
		stu[i].sum = stu[i].moral + stu[i].talent;
		if (stu[i].moral < L || stu[i].talent < L) {
			stu[i].flag = 5;
			m++;
		}
		else if (stu[i].moral >= H && stu[i].talent >= H)stu[i].flag = 1;
		else if (stu[i].moral >= H && stu[i].talent < H)stu[i].flag = 2;
		else if (stu[i].moral >= stu[i].talent)stu[i].flag = 3;
		else stu[i].flag = 4;
	}
	sort(stu, stu + n, cmp);
	printf("%d\n", n - m);//及格人数
	for (int i = 0; i < n - m; i++) {
		printf("%s %d %d\n", stu[i].id, stu[i].moral, stu[i].talent);
	}
	return 0;
}

发布了101 篇原创文章 · 获赞 1 · 访问量 2987

猜你喜欢

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