题目:写个程序,找出镇上最年长和最年轻的人。(今天是 2014 年 9 月 6 日)
输入格式:
第一行:给出正整数N
随后N行:每行给出1个人的姓名name,生日birthday
注:
超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。
题目保证最年长和最年轻的人没有并列。
输出格式:
在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。
思路:由题意知,合理的生日范围为1814/09/06 -- 2014/09/06,所以可以通过判断所给的生日是否在合理范围内来判断该生日是否合理,并基于选择排序的思想,找到最年长和最年轻的人。
代码:
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;cin >> n;
int count = 0;
string name,birthday;
string maxAgeName,minAgeName;
string maxAgeBirthday,minAgeBirthday;
while(cin >> name >> birthday){
// 1814/09/06 -- 2014/09/16
if(birthday.compare("1814/09/06") >= 0 && birthday.compare("2014/09/06") <= 0){
// 在合理的范围内
count += 1;
if(maxAgeName.empty() || birthday.compare(maxAgeBirthday) == -1){
maxAgeName = name;
maxAgeBirthday = birthday;
}
if(minAgeName.empty() || birthday.compare(minAgeBirthday) == 1){
minAgeName = name;
minAgeBirthday = birthday;
}
}
}
cout << count;
if(count != 0)
cout << " " << maxAgeName << " " << minAgeName << endl;
return 0;
}