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

被折叠的 条评论
为什么被折叠?



