码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-01-30 01:59:01      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


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

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5169978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!