14200: 【原4200】人口普查
题目
题目描述
author: 程序设计思想与方法助教组李泽仁王志伟 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4200 ## 问题描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的。假设已知镇上没有超过 120 岁(<=120)的老人,而今天是 2018年 12月 20日,所以超过 120岁的日期和未出生的日期都是不合理的,应该被过滤掉。
输入输出描述
输入
- 输入在第一行给出正整数 N,0<N<=1000;随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按
yyyy/mm/dd
(即年/月/日)格式给出的生日,姓名与生日之间用空格分开。题目保证最年长和最年轻的人没有并列(不存在两个以上相同的最值),保证无重名。
输出
- 在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。
要求
为方便检验你面向对象编程的能力,现提供相关类的声明和主函数以供参考。类的声明可以不一样,但要求使用给出的主函数(main)完成程序设计,不要另外编写主函数。
class Person {
public:
Person() {}
bool isLegal(); //判断输入是否合理,合理返回true,否则返回flase
void read(); //读入居民信息
// 可以根据设计自行添加所需成员
private:
char name[6]; //姓名
char birthday[11]; //生日
// 可以根据设计自行添加所需成员
};
class Solution{
public:
Solution() { count = 0;}
void regPerson(const Person &person);//统计有效生日人数,以及最年轻与最年长的人
void displayResult(); //输出相关信息
// 可以根据设计自行添加所需成员
private:
char eldestName[6]; //最年长人的姓名
char eldestBirthday[11]; //最年长人的生日
char youngestName[6]; //最年轻人的姓名
char youngestBirthday[11]; //最年轻人的生日
int count; //有效生日人数
};
//main函数不要做任何修改
int main()
{
Solution solution;
int num;
cin >> num;
for (int i = 0; i < num; i++){
Person person;
person.read();
if (person.isLegal()){
solution.regPerson(person);
}
}
solution.displayResult();
return 0;
}
程序运行示例0
Sample Input 0
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
Sample Output 0
2 Steve John
程序运行示例1
Sample Input 1
3
John 2018/12/20
Tom 1814/09/06
Ann 2121/01/30
Sample Output 1
1 John John
程序运行示例2
Sample Input 2
2
John 2018/12/20
Tom 1899/09/06
Sample Output 2
2 Tom John
ligongzzz's solution
#include "iostream"
#include "cstdio"
#include "cstdlib"
#include "algorithm"
#include "cstring"
using namespace std;
class Person {
public:
Person() {}
bool isLegal(); //判断输入是否合理,合理返回true,否则返回flase
void read(); //读入居民信息
// 可以根据设计自行添加所需成员
friend class Solution;
private:
char name[6]; //姓名
char birthday[11]; //生日
// 可以根据设计自行添加所需成员
};
void Person::read() {
cin >> name >> birthday;
}
bool Person::isLegal() {
int year, month, day;
sscanf(birthday, "%d/%d/%d", &year, &month, &day);
//判断不合理
if (month > 12 || day > 31)
return false;
int yearTime = year * 10000 + month * 100 + day;
if (yearTime > 20181220 || yearTime < 18981220)
return false;
else
return true;
}
class Solution {
public:
Solution() { count = 0; }
void regPerson(const Person &person);//统计有效生日人数,以及最年轻与最年长的人
void displayResult(); //输出相关信息
// 可以根据设计自行添加所需成员
private:
char eldestName[6]; //最年长人的姓名
char eldestBirthday[11]; //最年长人的生日
char youngestName[6]; //最年轻人的姓名
char youngestBirthday[11]; //最年轻人的生日
int count; //有效生日人数
};
void Solution::displayResult() {
cout << count << " " << eldestName << " " << youngestName;
}
void Solution::regPerson(const Person &person) {
count++;
int year, month, day;
int yearTime = 0, eldestYearTime = 0, youngestYearTime = 0;
sscanf(person.birthday, "%d/%d/%d", &year, &month, &day);
yearTime = year * 10000 + month * 100 + day;
if (count > 1) {
sscanf(eldestBirthday, "%d/%d/%d", &year, &month, &day);
eldestYearTime = year * 10000 + month * 100 + day;
sscanf(youngestBirthday, "%d/%d/%d", &year, &month, &day);
youngestYearTime = year * 10000 + month * 100 + day;
}
//判断是否最大最小
if (yearTime < eldestYearTime || count == 1) {
strcpy(eldestName, person.name);
strcpy(eldestBirthday, person.birthday);
}
if (yearTime > youngestYearTime || count == 1) {
strcpy(youngestName, person.name);
strcpy(youngestBirthday, person.birthday);
}
}
//main函数不要做任何修改
int main()
{
Solution solution;
int num;
cin >> num;
for (int i = 0; i < num; i++) {
Person person;
person.read();
if (person.isLegal()) {
solution.regPerson(person);
}
}
solution.displayResult();
return 0;
}