1028 人口普查 C++

在这里插入图片描述
思路分析:

这道题本身是很有意义的,只是被简化了反倒显得很无趣。

请让我们站在实际的情况下思考这个问题:

1. 设如果最年长和最年轻的人有并列,则按字母序输出名字;
2. 保证镇上每个人的名字是唯一的,即名字作为个人ID,不存在重名现象;

其他条件不变,请尝试编程实现。

先贴出原题示例代码:

#include<iostream>
#include<set>
using namespace std;
class Person {
public:
	Person() {
	}
	Person(const string& name,const string& birthday) {
		this->name = name;
		this->birthday = birthday;
	}
public:
	string name;
	string birthday;
};
//仿函数,指导set排序
struct  SortPerson
{
	bool operator()(const Person& p1, const Person& p2) {//日期升序排列
		
		if (p1.birthday < p2.birthday) {
			return true;
		}
		else if (p1.birthday == p2.birthday) {
			return p1.name < p2.name;
		}
		return false;
	}
};
int main() {
	//由于日期格式是一致的,使用string就可以判断大小
	int num;
	cin >> num;
	int all = 0;
	set<Person, SortPerson> se;
	for (int i = 0; i < num; i++) {//存元素
		string name;
		string birthday;
		cin >> name >> birthday;

		if (birthday >= "1814/09/06" && birthday <= "2014/09/06") {//日期合理
			Person p(name, birthday);
			all++;
			se.insert(p);
		}
	}
	if (se.size() == 0) {
		cout << 0;
		return 0;
	}
	Person youngest = *se.rbegin();//指向最后一个元素的反向迭代器
	Person oldest = *se.begin();
	cout << all << " " << oldest.name << " " << youngest.name;
	return 0;
}

下面是变形题的代码:

#include<iostream>
#include<set>
#include<vector>
using namespace std;
/*
    **************这里是变形题的代码,请不要搞错了!!******************
*/
class Person {
public:
	Person() {
	}
	Person(const string& name,const string& birthday) {
		this->name = name;
		this->birthday = birthday;
	}
public:
	string name;
	string birthday;
};
//仿函数,指导set排序
struct  SortPerson
{
	bool operator()(const Person& p1, const Person& p2) {//日期升序排列
		
		if (p1.birthday < p2.birthday) {
			return true;
		}
		else if (p1.birthday == p2.birthday) {
			return p1.name < p2.name;
		}
		return false;
	}
};
int main() {
	//由于日期格式是一致的,使用string就可以判断大小
	int num;
	cin >> num;
	set<Person, SortPerson> se;
	for (int i = 0; i < num; i++) {//存元素
		string name;
		string birthday;
		cin >> name >> birthday;

		if (birthday >= "1814/09/06" && birthday <= "2014/09/06") {//日期合理
			Person p(name, birthday);
			se.insert(p);
		}
	}
	if (se.size() == 0) {
		cout << 0;
		return 0;
	}
	Person youngest = *se.rbegin();//指向最后一个元素的反向迭代器
	Person oldest = *se.begin();
	vector<Person> youns,olds;//存放年龄最小和最大的人
	//筛选年龄最大和最小的人们
	for (set<Person, SortPerson>::iterator it = se.begin(); it != se.end(); it++) {
		if ((*it).birthday <= oldest.birthday) {
			olds.push_back(*it);
			oldest = *it;
		}
		else if ((*it).birthday >= youngest.birthday) {
			youns.push_back(*it);
			youngest = *it;
		}
	}
	//输出
	cout << se.size() << " ";//输出日期合理总人数
	if (youns.size() > 0) {
		//输出年长的人
	    for (int i = 0; i < olds.size(); i++) {
		    cout << olds[i].name<<" ";
	    }
 	    //输出年少的人
 		for (int i = 0; i < youns.size() - 1; i++) {
			cout << youns[i].name << " ";
		}
		cout << youns[youns.size() - 1].name;
		return 0;
	}
	else {
		//最年长与最年少的为同一人
		cout << olds[olds.size() - 1].name<<" ";
		cout << olds[olds.size() - 1].name;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值