#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;
}
1028 人口普查 (20 分)测试点4答案错误 求解
最新推荐文章于 2022-01-27 22:22:58 发布
本文介绍了一个C++程序,用于从用户输入中解析生日信息,计算并验证年龄,然后找出一组人员中年龄最大和最小的人。程序使用了结构体来存储个人数据,包括姓名、生日、年、月、日和年龄,并通过比较函数确定年龄的大小。
摘要由CSDN通过智能技术生成