1028. 人口普查(20)
题目:
某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉
输入格式
输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个英文字母组成的字符串)、以及按“yyyy/mm/dd”(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。
输出格式
在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。
输入样例
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
输出样例
3 Tom John
思路:
1.结构体存储信息:
struct Person
{
char name[7];
int yy, mm, dd;
};
2.不断更新最年长的人和最年轻的人。初始可以将第一个有效的信息作为初始,也可以最年长初始为2014年9月6日,最年轻 为1814年9月6日
3.为增强程序可读性,可考虑ME(Person a, Person b)和LE(Person a,Person b)函数
代码:
version1.0
/**
* @tag PAT_B_1028
* @authors R11happy (xushuai100@126.com)
* @date 2016-08-10 22:22:16-23:08
* @version 1.0
* @Language C++
* @Ranking 235/2964
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
struct Person
{
char name[7];
int yy, mm, dd;
};
int main()
{
Person Max, Min, st;
int N;
int cnt = 0;
scanf("%d", &N);
for (int i = 0; i<N; i++)
{
scanf("%s %d/%d/%d", &st.name, &st.yy, &st.mm, &st.dd);
//排除年龄过小的
if (st.yy > 2014 || ((st.yy == 2014) && ((st.mm>9) || ((st.mm == 9) && (st.dd > 6))))) continue;
//排出年龄过大的
if (st.yy < 1814 || ((st.yy == 1814) && ((st.mm<9) || ((st.mm == 9) && (st.dd < 6))))) continue;
//统计年龄合理的人数
cnt++;
//如果是第一个,初始化
if (cnt == 1) //最开始写成了if(i==0)
{
Max = st;
Min = st;
continue;
}
//找到年龄更小的
if (st.yy > Min.yy || ((st.yy == Min.yy) && ((st.mm>Min.mm) || ((st.mm == Min.mm) && (st.dd > Min.dd)))))
{
Min = st;
}
//找到年龄更大的
if (st.yy < Max.yy || ((st.yy == Max.yy) && ((st.mm<Max.mm) || ((st.mm == Max.mm) && (st.dd < Max.dd)))))
{
Max = st;
}
}
//注意处理没有合理年龄的情况
if (cnt > 0)
{
printf("%d %s %s", cnt, Max.name, Min.name);
}
else
printf("%d", cnt);
return 0;
}
version2.0
/**
* @tag PAT_B_1028
* @authors R11happy (xushuai100@126.com)
* @date 2016-08-10 22:22:16-23:08
* @version 2.0
* @Language C++
* @Ranking 235/2964
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
//结构体存储数据
struct Person
{
char name[7];
int yy, mm, dd;
}oldest, youngest, left, right, st;
//比较a是否LessOrEqual b
bool LE(Person a, Person b)
{
if (a.yy != b.yy) return a.yy <= b.yy;
else if (a.mm != b.mm) return a.mm <= b.mm;
else return a.dd <= b.dd;
}
//比较a是否MoreOrEqual b
bool ME(Person a, Person b)
{
if (a.yy != b.yy) return a.yy >= b.yy;
else if (a.mm != b.mm) return a.mm >= b.mm;
else return a.dd >= b.dd;
}
//初始化
void init()
{
youngest.yy = left.yy = 1814;
oldest.yy = right.yy = 2014;
youngest.mm = oldest.mm = left.mm = right.mm = 9;
youngest.dd = oldest.dd = left.dd = right.dd = 6;
}
int main()
{
init();
int N;
int cnt = 0; //统计合理的年龄
scanf("%d", &N);
while (N--)
{
scanf("%s %d/%d/%d", &st.name, &st.yy, &st.mm, &st.dd);
if (LE(st, right) && ME(st, left))
{
cnt++;
if (LE(st, oldest)) oldest = st;
if (ME(st, youngest)) youngest = st;
}
}
if (cnt > 0) printf("%d %s %s", cnt, oldest.name, youngest.name);
else printf("%d", cnt);
return 0;
}
收获:
1.考虑所有人年龄都不合理的情况,要单独讨论:
if (cnt > 0) printf("%d %s %s", cnt, oldest.name, youngest.name);
else printf("%d", cnt);
2.在使用 新读入的日期来更新最大日期和最小日期时,有可能同时更新最大日期和最小日期,因此不能使用if……else的写法来选择其中一个更新
3.日期的比较注意要分三种情况讨论:
//比较a是否LessOrEqual b
bool LE(Person a, Person b)
{
if (a.yy != b.yy) return a.yy <= b.yy;
else if (a.mm != b.mm) return a.mm <= b.mm;
else return a.dd <= b.dd;
}