1. 程式人生 > >1028.人口普查(PAT)

1028.人口普查(PAT)

bsp 題目 while true 註意 int bool pri ret

題目分析:構造一個Compare函數,比較日期的大小即可,不過要註意如果沒有合法的生日,就只能輸出一個零(這裏賊坑)。

代碼如下

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct person{
 5     char name[20];
 6     int year;
 7     int month;
 8     int day;
 9 }youngest,oldest;
10 
11 void Init(){
12     youngest.year = 2014;
13     youngest.month = 9
; 14 youngest.day = 6; 15 oldest.year = 1814; 16 oldest.month = 9; 17 oldest.day = 6; 18 } 19 bool compareTo(person &p1,person &p2){ 20 if(p1.year>p2.year) 21 return false; 22 if(p1.year<p2.year) 23 return true; 24 if(p1.year==p2.year){ 25 if
(p1.month>p2.month) 26 return false; 27 if(p1.month<p2.month) 28 return true; 29 if(p1.month==p2.month){ 30 if(p1.day>p2.day) 31 return false; 32 if(p1.day<p2.day) 33 return true; 34 if
(p1.day==p2.day) 35 return false; 36 } 37 } 38 } 39 40 int main(){ 41 Init(); 42 int n; 43 person temp,min,max; 44 min = oldest; 45 max = youngest; 46 scanf("%d",&n); 47 int acount = 0; 48 while(n--){ 49 scanf("%s %d/%d/%d",&temp.name,&temp.year,&temp.month,&temp.day); 50 if(compareTo(youngest,temp)||compareTo(temp,oldest)) 51 continue; 52 else{ 53 acount++; 54 if(compareTo(min,temp)) min = temp; 55 if(compareTo(temp,max)) max = temp; 56 } 57 } 58 if(acount!=0) 59 printf("%d %s %s\n",acount,max.name,min.name); 60 else printf("0\n"); 61 return 0; 62 }

1028.人口普查(PAT)