PAT (Basic Level) Practise:1028. 人口普查

【题目链接】

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

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过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 #include <stdio.h>
 2 #include <string.h>
 3 
 4 // yyyy/mm/dd
 5 int is_a_older_b(char *a, char *b)
 6 {
 7     int i;
 8     for(i = 0; i < 10; i++)
 9     {
10         if(b[i] - a[i] > 0)
11             return 1;
12         if(b[i] - a[i] < 0)
13             return 0;
14     }
15     return 0;
16 }
17 
18 int main(void)
19 {
20     int N;
21     int i, cnt;
22 
23     char youngest[12] = "1814/09/05";
24     char oldest[12]   = "2014/09/07";
25 
26     char youngest_name[10];
27     char oldest_name[10];
28 
29     char name[10];
30     char birthday[12];
31 
32     scanf("%d", &N);
33 
34     cnt = 0;
35     for(i = 0; i < N; i++)
36     {
37         scanf("%s %s", name, birthday); 
38         
39         if(is_a_older_b("1814/09/05", birthday) && is_a_older_b(birthday, "2014/09/07"))
40         {
41             cnt++;
42 
43             if(is_a_older_b(birthday, oldest))
44             { 
45                 strcpy(oldest, birthday);
46                 strcpy(oldest_name, name);
47             }
48 
49             if(is_a_older_b(youngest, birthday))
50             {
51                 strcpy(youngest, birthday);
52                 strcpy(youngest_name, name);
53             }
54         }
55     }
56 
57     if(cnt > 0)
58         printf("%d %s %s", cnt, oldest_name, youngest_name);
59     else
60         printf("0");
61 
62     return 0;
63 }
原文地址:https://www.cnblogs.com/utank/p/4762554.html