pat 1028. 人口普查(20)

本文深入分析了一段C++代码,该代码用于验证人口数据中年龄的有效性,并通过比较预设的最年轻和最老年的日期来筛选有效记录。详细介绍了代码的结构、逻辑以及如何在循环中迭代处理输入数据,最终输出有效记录的数量及其对应人员的名称。
#include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>


using namespace std;


int validCount = 0;
string oldest("1814/09/06");
string youngest("2014/09/06");


typedef struct person {
string name;
string birthday;
} Person;


bool isValid(Person p) {
return p.birthday.compare(youngest) <= 0 
&& p.birthday.compare(oldest) >= 0;
}


int main() {
int n;
int i;
while((cin >> n) != NULL) {
Person youngPer;
youngPer.birthday = oldest;
Person oldPer;
oldPer.birthday = youngest;
for (i = 0; i < n; i++) {
Person p;
cin >> p.name >> p.birthday;
if (!isValid(p)) continue;
validCount++;
if (p.birthday.compare(oldPer.birthday) < 0) {
oldPer = p;
}
if (p.birthday.compare(youngPer.birthday) > 0) {
youngPer = p;
}
}
if (validCount > 0)
cout << validCount << " " << oldPer.name << " " << youngPer.name << endl;
else
cout << validCount << endl;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值