PAT(简单题):人口普查。一个测试点没过,求思路
链接:https://www.patest.cn/contests/pat-b-practise/1028
#include<stdio.h>
#include<cstring>
struct people
{
char name[10];
char birthday[11];
//people():name(""),birthday(""){}
};
int main()
{
int n,count=0;
scanf("%d",&n);
char today[11]="2014/09/06";
char lower_bound[11]="1814/09/06";
struct people maxD={"","2014/09/06"},minD={"","1814/09/06"};//maxD = the oldest, minD=the youngest;
for(int i=0;i<n;i++)
{
struct people s;
scanf(" %s%s",s.name,s.birthday);
if(strcmp(s.birthday,today)>0 || strcmp(s.birthday,lower_bound)==-1) continue;//find the worry date;
count++;
if(strcmp(s.birthday,maxD.birthday)<0) maxD=s;//s is older than maxD
if(strcmp(s.birthday,minD.birthday)>0) minD=s;//s is younger than minD
}
printf("%d",count);
if(count == 1)
if(strcmp(maxD.name,minD.name)>0) printf(" %s %s\n",maxD.name,maxD.name);//when count=1
else printf(" %s %s\n",minD.name,minD.name);
if(count > 1) printf(" %s %s\n",maxD.name,minD.name);
return 0;
}
#include<cstring>
struct people
{
char name[10];
char birthday[11];
//people():name(""),birthday(""){}
};
int main()
{
int n,count=0;
scanf("%d",&n);
char today[11]="2014/09/06";
char lower_bound[11]="1814/09/06";
struct people maxD={"","2014/09/06"},minD={"","1814/09/06"};//maxD = the oldest, minD=the youngest;
for(int i=0;i<n;i++)
{
struct people s;
scanf(" %s%s",s.name,s.birthday);
if(strcmp(s.birthday,today)>0 || strcmp(s.birthday,lower_bound)==-1) continue;//find the worry date;
count++;
if(strcmp(s.birthday,maxD.birthday)<0) maxD=s;//s is older than maxD
if(strcmp(s.birthday,minD.birthday)>0) minD=s;//s is younger than minD
}
printf("%d",count);
if(count == 1)
if(strcmp(maxD.name,minD.name)>0) printf(" %s %s\n",maxD.name,maxD.name);//when count=1
else printf(" %s %s\n",minD.name,minD.name);
if(count > 1) printf(" %s %s\n",maxD.name,minD.name);
return 0;
}
我的思路就是字符串比较,年龄大的字符串(生日)较小,年龄小的字符串(生日)较大。
要考虑的有,当输入都是不合理的生日,即没有有效的生日;输入只有一个有效的生日;输入只有一个有效的生日且该生日为边界(为2014/09/06或1814/09/06);
疑惑:有一个测试点没通过,想了一下午还是不知道,求空闲的大佬看看哪种情况没考虑到。
[此贴子已经被作者于2018-4-9 18:02编辑过]