PTA basic 1028 人口普查 (20 分) c语言实现(gcc)
某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。
这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。
输入格式:
输入在第一行给出正整数 N,取值在(;随后 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
和月饼题相似,
检测点12是边界值检测,分别是1814 09 06 和2014 09 06
检测点3是当没有正常数据时,仅输出0
检测点4是极限数据的处理效率
1 #include "stdio.h"
2 #include "stdlib.h"
3 #include "string.h"
4 typedef struct civil{
5 int year;
6 int mon;
7 int day;
8 char name[6];
9 } civilList;
10
11 int compare(const void *l,const void *r){
12 civilList *lp=(civilList *)l;
13 civilList *rp=(civilList *)r;
14 int result=0;
15 if((lp->year>rp->year)||(lp->year==rp->year&&lp->mon>rp->mon)||(lp->year==rp->year&&lp->mon==rp->mon&&lp->day>rp->day)){
16 result=-1;
17 }else{
18 result=1;
19 }
20 return result;
21 }
22
23 int main(){
24 int n,i,day,mon,year,count=0;
25 char name[6];
26 scanf("%d",&n);
27 civilList cl[n];
28 for(i=0;i<n;i++){
29 scanf("%s %d/%d/%d",name,&year,&mon,&day);
30 if((year>1814&&year<2014)||(year==1814&&mon>9)||(year==1814&&mon==9&&day>=6)||(year==2014&&mon<9)||(year==2014&&mon==9&&day<=6)){
31 strcpy(cl[count].name,name);
32 cl[count].year=year;
33 cl[count].mon=mon;
34 cl[count].day=day;
35 count++;
36 }
37 }
38 qsort(cl,count,sizeof(civilList),compare);
39 if(count){
40 printf("%d %s %s\n",count,cl[count-1].name,cl[0].name);
41 }else{
42 printf("0");
43 }
44 return 0;
45 }
