注册 登录
编程论坛 C++教室

PAT(简单题):人口普查。一个测试点没过,求思路

moox 发布于 2018-04-06 17:59, 1864 次点击
链接:https://www.
程序代码:
#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;
}

我的思路就是字符串比较,年龄大的字符串(生日)较小,年龄小的字符串(生日)较大。
要考虑的有,当输入都是不合理的生日,即没有有效的生日;输入只有一个有效的生日;输入只有一个有效的生日且该生日为边界(为2014/09/06或1814/09/06);

疑惑:有一个测试点没通过,想了一下午还是不知道,求空闲的大佬看看哪种情况没考虑到。

[此贴子已经被作者于2018-4-9 18:02编辑过]

2 回复
#2
rjsp2018-04-08 09:05
strcmp 的返回值是 小于0、等于0、大于零 吧,没听说过是 -1、0、+1
#3
moox2018-04-09 09:09
回复 2楼 rjsp
可是我电脑上测试显示前面字符串时为1,后面大为-1,我就这样写了
1