解题思路:此题我想到俩种方法,一种是排序方法,一种是不排序的方法,首先都是看是否是有效年龄,然后一种是排序,另一种是维护最大值和最小值的变量,一定要注意如果有效数字是0那就只输出0就可以了。
c语言解法一:排序法
#include<stdio.h>
typedef struct
{
char name[6];
int year;
int month;
int day;
}old_man;
int valid(old_man m)
{
if(m.year>2014||m.year<2014-200)
return 0;
if(m.year==2014&&(m.month>9||(m.month==9&&m.day>6)))
{
return 0;
}
if(m.year==2014-200&&(m.month<9||(m.month==9&&m.day<6)))
return 0;
return 1;
}
int compare(old_man a,old_man b)
{
if(a.year!=b.year)
return b.year-a.year;
if(a.month!=b.month)
return b.month-a.month;
return b.day-a.day;
}
int main()
{
int n,youxiao=0,i;
old_man max,min,current,temp;
max.day=31,max.year=2015,max.month=12;
min.day=0,min.year=0,min.month=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s %d/%d/%d",current.name,&(current.year),&(current.month),&(current.day));
if(valid(current))
youxiao++;
else
continue;
if(compare(max,current)<0)
{
max=current;
}
if(compare(current,min)<0)
{
min=current;
}
}
if(youxiao)
printf("%d %s %s\n",youxiao,max.name,min.name);
else
printf("%d\n",youxiao);
return 0;
}
c语言解法二不排序:
#include<stdio.h>
typedef struct
{
char name[6];
int year;
int month;
int day;
}old_man;
int valid(old_man m)
{
if(m.year>2014||m.year<2014-200)
return 0;
if(m.year==2014&&(m.month>9||(m.month==9&&m.day>6)))
{
return 0;
}
if(m.year==2014-200&&(m.month<9||(m.month==9&&m.day<6)))
return 0;
return 1;
}
int compare(old_man a,old_man b)
{
if(a.year!=b.year)
return b.year-a.year;
if(a.month!=b.month)
return b.month-a.month;
return b.day-a.day;
}
int main()
{
int n,youxiao=0,i;
old_man max,min,current,temp;
max.day=31,max.year=2015,max.month=12;
min.day=0,min.year=0,min.month=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s %d/%d/%d",current.name,&(current.year),&(current.month),&(current.day));
if(valid(current))
youxiao++;
else
continue;
if(compare(max,current)<0)
{
max=current;
}
if(compare(current,min)<0)
{
min=current;
}
}
if(youxiao)
printf("%d %s %s\n",youxiao,max.name,min.name);
else
printf("%d\n",youxiao);
return 0;
}