1028 人口普查 (20 分)测试点4答案错误 求解

#include<iostream>
#include<string>
#include<vector>
using namespace std;

void calculateAge(struct person&);
void preprocessInfo(struct person&);
bool compareAge(const struct person&, const struct person&);

struct person
{
	string name;
	string birthday;
	int year,month,day,age;
	bool validity;
};
const int now_year = 2014, now_month = 9, now_day = 6;
int main()
{
	int N;
	scanf_s("%d", &N);
	vector<person>data;
	person p;
	for (int i = 0; i < N; i++)
	{
		cin >> p.name >> p.birthday;
		preprocessInfo(p);
		if(p.validity)data.push_back(p);
		//data.push_back(p);
	}
	if (data.size() != 0)
	{
		person max = data[0], min = data[0];
		for (unsigned int i = 0; i < data.size(); i++)
		{
			if (compareAge(data[i], max))
			{
				max = data[i];
				//cout << max.name << " " << max.age<<endl;
			}
			if (compareAge(min, data[i]))min = data[i];
		}
		cout << data.size() << " " << max.name << " " << min.name;
	}
	else cout << 0;
	return 0;
}
void preprocessInfo(struct person& p)
{
	p.year= stoi(p.birthday.substr(0, 4));
	p.month = stoi(p.birthday.substr(5, 2));
	p.day = stoi(p.birthday.substr(8, 2));
	
	calculateAge(p);
	if (p.age != -1)
	{
		if (p.age <= 200)p.validity = true;
		else p.validity = false;  
	}
	else p.validity = false;
}
void calculateAge(struct person& p)
{
	if (p.year > now_year)
	{
		p.age = -1;
		return;
	}
	/* p.year <= now_year */
	if (p.year == now_year)
	{
		if (p.month < now_month)
		{
			p.age = 1;
			return;
		}
		if (p.month > now_month)
		{
			p.age = -1;
			return;
		}
		if (p.month == now_month)
		{
			if (p.day > now_day)
			{
				p.age = -1;
				return;
			}
			if (p.day == now_day)
			{
				p.age = 0;
				return;
			}
			p.age = 1;
			return;
		}
	}
	/* p.year < now_year */
	if (p.month > now_month)
	{
		p.age = now_year - p.year;
		return;
	}
	if (p.month == now_month)
	{
		if (p.day < now_day)
		{
			p.age = now_year - p.year+1;
			return;
		}
		p.age = now_year - p.year;
		return;
	}
	if (p.month < now_month)
	{
		p.age = now_year - p.year+1;
	}
	return;
}
bool compareAge(const struct person& p1, const struct person& p2) //p1>p2 true else:false
{
	if (p1.age > p2.age)return true;
	if (p1.age == p2.age)
	{
		if (p1.month < p2.month)return true;
		if (p1.month == p2.month)
		{
			if (p1.day < p2.day)return true;
		}
	}
	return false;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值