【题解】PATB-1028 人口普查

人口普查 (PATB-1028)

题面

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入

输入在第一行给出正整数 N,取值在(0,105];随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

样例输入

1
2
3
4
5
6
5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

样例输出

1
3 Tom John

提示

思路

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
struct P{
char s[15], t[15];
}tmp, mx, mi;

int main()
{
int n;
scanf("%d", &n);

int num = 0;
for(int i=0; i<n; i++)
{
scanf("%s %s", tmp.s, tmp.t);

if(strcmp(tmp.t, "1814/09/06") < 0)
continue;
if(strcmp(tmp.t, "2014/09/06") > 0)
continue;
num++;

if(num==1)
{
strcpy(mi.s, tmp.s); strcpy(mi.t, tmp.t);
strcpy(mx.s, tmp.s); strcpy(mx.t, tmp.t);
continue;
}

if(strcmp(tmp.t, mi.t) < 0) {
strcpy(mi.s, tmp.s); strcpy(mi.t, tmp.t);
}

if(strcmp(tmp.t, mx.t) > 0) {
strcpy(mx.s, tmp.s); strcpy(mx.t, tmp.t);
}
}
if(num)
printf("%d %s %s\n", num, mi.s, mx.s);
else
printf("0\n");

return 0;
}