PAT乙级 1015 德才论

把当时的代码放出来,大家共同学习,互相帮助
题目:
在这里插入图片描述
输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

这道题掌握了方法就不难,关键是得用好一些数据结构,这道题vector容器+结构体+sort()函数真是绝配,还有最好不要用python写,解释型语言不适合做算法题,动不动就超时,我用python写这道题又3个测试点都超时,主要是我找不到合适的容器,写的比较繁琐,就不放代码了,柳婼小姐姐的代码很清晰,我就写点注释吧= ̄ω ̄=(C/C++):

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {  // 定义结构体有3个数据成员:准考证号,德分,才分
	int num, de , cai;  
}; 
int cmp(struct node a, struct node b) {  // 定义结构体node对象的比较函数
	if (a.de + a.cai != b.de + b.cai)  // 总分不等按总分降序排序
		return (a.de + a.cai) > (b.de + b.cai);
	else if (a.de != b.de)  // 总分相等,德分不等按德分降序排序
	    return a.de > b.de;
	else  // 总分得分均相等,按考号升序排列
	    return a.num < b.num;
}
int main() {
	int n, low, high;  // 人数, 最低线, 优先线
	scanf("%d %d %d", &n, &low, &high);
	vector<node> v[4];  // 定义一个盛放node对象的二维vector数组v
	node temp;  // 结构体对象
	int total = n;  // 总人数
	for (int i = 0; i < n; i++) {  
		scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
		if (temp.de < low || temp.cai < low)  // 未达最低线的
		    total--;  // 总人数中减去未达最低线的
		else if (temp.de >= high && temp.cai >= high)  // 第一等
		    v[0].push_back(temp);
		else if (temp.de >= high && temp.cai < high)  // 第二等
		    v[1].push_back(temp);
		else if (temp.de < high && temp.cai < high && temp.de >= temp.cai)  // 第三等
		    v[2].push_back(temp);
		else  // 第四等
		    v[3].push_back(temp);
	}   
	printf("%d\n", total);  // 输出达到最低线的人数
	for (int i = 0; i < 4; i++) {
		sort(v[i].begin(), v[i].end(), cmp);  // 按cmp函数排序
		for (int j = 0;j < v[i].size(); j++)  // 遍历输出v中排序后的元素
		    printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
	}
}
发布了75 篇原创文章 · 获赞 34 · 访问量 2028

猜你喜欢

转载自blog.csdn.net/chongchujianghu3/article/details/105152937