PAT Basic Level 1028

AC代码

 1 /*此题注意理解重载运算符的重要性*/ 
 2 /*直接写入程序以后若要相关修改较繁琐*/ 
 3 /*搞清楚什么时候要用到数组,为什么此题可以不用*/
 4 /*此题卡在COMPARE函数中,注意IF ELSE 仔细思考*/ 
 5 #include <stdio.h>
 6 struct People
 7 {
 8     char name[10];
 9     int year;
10     int month;
11     int day;
12  } ;
13 typedef struct People People;
14 int Compare(People p1,People p2)        //p1与p2相比 (返回1代表老,返回2代表相等,返回0代表小于) 
15 {                                         
16     int ret = 2;
17     if(p1.year < p2.year)
18     {
19         ret = 1;
20     }else
21     if(p1. year == p2.year)
22     {
23         if(p1.month < p2.month)
24         {
25             ret = 1;
26         }else
27         if(p1.month == p2.month)
28         {
29             if(p1.day < p2.day)
30             {
31                 ret = 1;
32             }else
33             if(p1.day == p2.day)
34             {
35                 ret = 2;
36             }else
37             ret = 0;
38         }else
39         ret = 0;
40     }else
41     ret = 0;
42     return ret;
43 }
44 int main ()
45 {
46     int amount;
47     People oldest = {.year = 2014,.month = 9,.day = 6};
48     People youngest = {.year = 1814,.month =  9,.day = 6};
49     People old = {.year = 1814,.month =  9,.day = 6};         //必须要年轻于或等于这个日期 
50     People young = {.year = 2014,.month = 9,.day = 6};       //必须老于或等于这个日期 
51 
52     People input;
53     int cnt = 0;
54     scanf("%d",&amount);
55     int i;
56     for(i = 0;i < amount;i++)
57     {
58         scanf("%s",&input.name);
59         scanf("%d/%d/%d",&input.year,&input.month,&input.day);
60         if((Compare(input,young) == 1 || Compare(input,young) == 2)&&(Compare(input,old) == 0 || Compare(input,old) == 2))      //大于等于1814 9 6 小于等于 2014 9 6 
61         {
62             cnt++;
63             if(Compare(input,youngest) == 0 || Compare(input,youngest) == 2)       //年轻于或等于最年轻的,则替换最年的数据 
64             {
65                 youngest = input;
66             }
67             if(Compare(input,oldest) == 1 ||Compare(input,oldest) == 2)          //老于或等于最年长的,则替换最年长的数据 
68             {
69                 oldest = input;
70             }
71             
72         }
73     }
74     if(cnt != 0)
75     {
76         printf("%d ",cnt);
77         printf("%s ",oldest.name);
78         printf("%s",youngest.name);
79     }else
80     if(cnt == 0)
81     {
82         printf("0");
83     }
84     return 0;
85     
86     
87     
88 }
原文地址:https://www.cnblogs.com/Ponytai1/p/5976533.html