Talent and Virtue (25)

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(圣人)”; being

less excellent but with one’s virtue outweighs talent can be called a
“nobleman(君子)”; being good in neither is a

“fool man(愚人)”; yet a fool man is better than a “small
man(小人)” who prefers talent than virtue. Now given the grades of
talent and virtue of a group of people, you are supposed to rank them
according to Sima

Guang’s theory.

输入描述:
Each input file contains one test case. Each case first gives 3 positive integers in a line: N (<=105), the total number of
people to be ranked; L (>=60), the lower bound of the qualified grades – that is, only the ones whose grades of talent and virtue are
both not below this line will be ranked; and H (<100), the higher line of qualification – that is, those with both grades not below this line
are considered as the “sages”, and will be ranked in non-increasing order according to their total grades. Those with talent grades below
H but virtue grades not are cosidered as the “noblemen”, and are also ranked in non-increasing order according to their total grades, but
they are listed after the “sages”. Those with both grades below H, but with virtue not lower than talent are considered as the “fool
men”. They are ranked in the same way but after the “noblemen”. The rest of people whose grades both pass the L line are ranked after
the “fool men”.

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade

where ID_Number is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.

输出描述:
The first line of output must give M (<=N), the total number of people that are actually ranked. Then M lines follow, each gives the
information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be
ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID’s.

输入例子:
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


大意描叙
给你一大堆数据,需要你按照特定的要求进行排序。
分析
这是一道简单题,困难之处在于阅读题目,理清各种情况,若能够正确并且全面地知道排序的要求,那么写出解答代码非常容易。排序可以用标准库的排序函数加上自己的重定义比较运算符。我的第一思路就是创建几个容器,分别装载几种人物代表的数据,最后对各个容器排序好输出即可。还有一种常见的解法是为结构体增加一个等级属性作为第一排序依据。还有一种比较巧妙的解法是在判断类型时加上一个特定值,例如德才兼备则总分加1000。
MyCode

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int maxn = 100009;
struct Man{
	string id;
	int vir;
	int talent;
	int total;
	bool operator < (const Man& n){
		if (total == n.total) {
			if(vir == n.vir) return id < n.id;
			else return vir > n.vir;
		}
		return total > n.total;
	}
	void print(){
		cout << id << " " << vir << " " << talent << endl;
	}
}man[maxn];
vector<Man> sege, noble, fool, eve;
void display(vector<Man>n){
	sort(n.begin(), n.end());
	for (int i = 0; i < n.size(); i++)
		n[i].print();
}
int main(){
	int n, s, m;
	int ans1 = 0;	//the number of man taken choise
	cin >> n >> s >> m;
	while (n--){
		Man temp;
		cin >> temp.id >> temp.vir >> temp.talent;
		temp.total = temp.vir + temp.talent;
		if (temp.vir >= s && temp.talent >= s){
			ans1++;
			if (temp.vir >= m && temp.talent >= m){	//sage
				sege.push_back(temp);
				continue;
			}
			else if (temp.vir >=m && temp.talent< m ){ //noble man
				noble.push_back(temp);
				continue;
			}
			else if (temp.talent < m && temp.vir< m && temp.vir>=temp.talent){
				fool.push_back(temp);
				continue;
			}
			else{
				eve.push_back(temp);
				continue;
			}
		}
	}
	cout << ans1 << endl;
	display(sege);
	display(noble);
	display(fool);
	display(eve);
	return 0;
}

回顾
这是第二次做这道题目,意思一样,不同的是这次是英文版。但是最终debug的时间竟然比写解答的时间要长。
原因是没有仔细看排序要求,竟然看漏了 “but with virtue not lower than talent” !所以说下次做题千万勿要确信自己
没有看错题意,有时重新仔细看题目是有必要的。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值