洛谷P1104 生日进阶解法

这是一个C++程序,用于读取n个人的姓名、生日和编码,并根据生日和编码进行升序排序。程序首先处理输入的日期格式,然后使用sort函数和自定义比较函数进行排序,最后打印出排序后的人员姓名。
#include<iostream>
#include<algorithm>
#include<string>

using namespace std;
struct Person
{
	int code;
	string name;
	string birthday;
}person[100];
int n;

bool compare(const Person& a, const Person& b)
{
	if (a.birthday < b.birthday || a.birthday == b.birthday && a.code>b.code)
		return true;
	else
		return false;
}

int main()
{
	string year, month, day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		person[i].code = i;
		cin >> person[i].name >> year >> month >> day;
		if (month.length() == 2 && day.length() == 2)
			person[i].birthday = year + month + day;
		else if (month.length() == 1 && day.length() == 2)
			person[i].birthday = year + '0' + month + day;
		else if (month.length() == 2 && day.length() == 1)
			person[i].birthday = year + month + '0' + day;
		else if (month.length() == 1 && day.length() == 1)
			person[i].birthday = year + '0' + month + '0' + day;
	}
	sort(person, person + n, compare);
	for (int i = 0; i < n; i++)
		cout << person[i].name << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值