PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)

1. 在排序的过程中,注意边界的处理(小于、小于等于)

2. 对于B-level,这题是比較麻烦一些了。

源代码:

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct People
{
	int m_id;
	int m_virtue;
	int m_talent;
	People(int id, int virtue, int talent): m_id(id), m_virtue(virtue), m_talent(talent) {}
	void print()
	{
		printf("%08d %d %d\n", m_id, m_virtue, m_talent);
	}
	friend bool operator< (const People& a, const People& b)
	{
		if (a.m_virtue+a.m_talent != b.m_virtue+b.m_talent)
		{
			return a.m_virtue+a.m_talent > b.m_virtue+b.m_talent;
		} else if (a.m_virtue != b.m_virtue)
		{
			return a.m_virtue > b.m_virtue;
		} else
		{
			return a.m_id < b.m_id;
		}
	}
};

vector<People> sage, noble_man, fool_man, small_man;
int n, l, h;
int id, virtue, talent;

int main()
{
	scanf("%d%d%d", &n, &l, &h);
	for (int i = 0; i < n; ++ i)
	{
		scanf("%d%d%d", &id, &virtue, &talent);
		if (virtue < l || talent < l)
		{
			continue;
		} else if (virtue >= h && talent >= h)
		{
			sage.push_back( People(id, virtue, talent) );
		} else if (virtue  >= h)
		{
			noble_man.push_back( People(id, virtue, talent) );
		} else if (virtue >= talent)
		{
			fool_man.push_back( People(id, virtue, talent) );
		} else
		{
			small_man.push_back( People(id, virtue, talent) );
		}
	}

	sort(sage.begin(), sage.end());
	sort(noble_man.begin(), noble_man.end());
	sort(fool_man.begin(), fool_man.end());
	sort(small_man.begin(), small_man.end());

	printf("%d\n", sage.size() + noble_man.size() + fool_man.size() + small_man.size());
	for (size_t i = 0; i < sage.size(); ++ i)
	{
		sage[i].print();
	}
	for (size_t i = 0; i < noble_man.size(); ++ i)
	{
		noble_man[i].print();
	}
	for (size_t i = 0; i < fool_man.size(); ++ i)
	{
		fool_man[i].print();
	}
	for (size_t i = 0; i < small_man.size(); ++ i)
	{
		small_man[i].print();
	}

	return 0;
}
时间: 2024-08-07 00:07:55

PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)的相关文章

PAT 1062 Talent and Virtue[难]

1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣

1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding i

PAT 1062. Talent and Virtue (25)

1062. Talent and Virtue (25) About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人

【PAT】1015 德才论 (25)(25 分)

1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的"德才论":"是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人." 现给出一批考生的德才分数,请根据司马光的理论给出录取排名. 输入格式: 输入第1行给出3个正整数,分别为:N(<=10^5^),即考生总数:L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被

PAT:1015. 德才论 (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char mID[10]; int de,cai,sum; int tag; //标明是第几类:1德才都们组,2德胜才,3 }STU[100010]; bool cmp(Student a,Student b) //[warning]不能写STU { if(a.tag!=b.tag)

[PAT Basic] 1015 德才论 (25 分)

思路 根据题意,首先创建对应结构体,分别定义四个vector,根据提交逐个push, 重点是sort比较函数的逻辑,然后对每个vector排序输出 cpp #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int n, l, h, suc = 0; typedef struct stu { int id; int

PAT 1062 Talent and Virtue

#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <algorithm> using namespace std; class Man { public: char id[10]; int talent; int virtue; }; bool mycmp(const Man& a, const Man& b) {

PAT (Advanced Level) 1062. Talent and Virtue (25)

简单排序.题意较长. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using namespace std; struct X { int id;

pat(A) 1062. Talent and Virtue(结构体排序)

代码: #include<cstdio> #include<cstring> #include<algorithm> #define N 100005 using namespace std; struct Node { int num; int v; int t; void Set(int x,int y,int z) { num=x; v=y; t=z; } }; int cmp(Node a,Node b) { if((a.t+a.v==b.v+b.t)&