P 1028 人口普查

转跳点:🐏

1028 人口普查
 

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。

输入格式:

输入在第一行给出正整数 N,取值在(;随后 N 行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按 yyyy/mm/dd(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

输出格式:

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

输入样例:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20
 

输出样例:

3 Tom John

 

  这道题就是简单的枝减法就可以完成了,也不用开数组,在比较过程中我们只需要记录下最大年纪的人最小年纪的人和输入值。注意所有人出生日期不合法的可能。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//比较喜欢宏定义,这样写代码移植性强
#define EARLIESTYEAR    1814
#define NOWYEAR         2014
#define NOWMONTH        9
#define NOWDAY          6

typedef struct
{
    char name[6];
    int year, month, day;
} PersonalData;

//输入出生日期合法判断
static int Islegal(PersonalData *temp);
//查找最大最小年龄的人
static void Find(PersonalData *MAX, PersonalData *MIN, const PersonalData *temp);

int main(void)
{
    int n, count = 0;
    PersonalData theoldest = {"\0", 2015, 12, 31}, theyoungest = {"\0", 1813, 1, 1};
    scanf("%d", &n);

    for (int i = 0; i < n; i++)
    {
        PersonalData temp;
        scanf("%s %d/%d/%d", temp.name, &temp.year, &temp.month, &temp.day);

        if (Islegal(&temp))
        {
            continue;
        }
        Find(&theoldest, &theyoungest, &temp);
        count++;
    }
    
    //这里可以直接加个if特判count == 0的情况,我脑子有坑才这么写
    printf("%d%s%s%s%s", count, 0 == count ? "" : " ", 0 == count ? "" : theoldest.name, 0 == count ? "" : " ", 0 == count ? "" : theyoungest.name);

    return 0;
}

static void Find(PersonalData *MAX, PersonalData *MIN, const PersonalData *temp)
{
    //分了三种情况,出生年月日小于于最大值的年月日的就交换
    //生的越早,年份越小,年纪越老
    if ((MAX->year > temp->year) ||
        (MAX->year == temp->year && MAX->month > temp->month) ||
        (MAX->year == temp->year && MAX->month == temp->month && MAX->day > temp->day))
    {
        *MAX = *temp;
    }
    //同理
    if ((MIN->year < temp->year) ||
        (MIN->year == temp->year && MIN->month < temp->month) ||
        (MIN->year == temp->year && MIN->month == temp->month && MIN->day < temp->day))
    {
        *MIN = *temp;
    }
}

static int Islegal(PersonalData *temp)
{
    //年超出的
    if (temp->year > NOWYEAR || temp->year < EARLIESTYEAR)
    {
        return 1;
    }
    //月份超出的
    if ((NOWYEAR == temp->year && temp->month > NOWMONTH) ||
        (EARLIESTYEAR == temp->year && temp->month < NOWMONTH))
    {
        return 1;
    }
    //日期超出的
    if ((NOWYEAR == temp->year && NOWMONTH == temp->month && temp->day > NOWDAY) ||
        (EARLIESTYEAR == temp->year && NOWMONTH == temp->month && temp->day < NOWDAY))
    {
        return 1;
    }

    return 0;
}

贡献一组测试用例

5
John 2051/05/12
Tom 1814/01/06
Ann 2121/01/30
James 1814/09/05
Steve 1671/11/20

结果

0  (没有空格~~~)

 

  PTA不易,诸君共勉!

posted @ 2020-01-13 11:45  秦_殇  阅读(155)  评论(0编辑  收藏  举报