1028 人口普查 (20 分)

在读入日期时判断该日期是否在合法日期的区间内,如果在,就使其更新最年长的人的出生日期和最年轻的人的出生日期。由于判断日期是否在合法日期区间
内、更新最年长和最年轻的信息都将涉及日期的比较操作,因此不妨写两个比较函数用来比较a与b的日期。

ps:有可能存在所有人的日期都不在合法区间内的情况,这时必须特判输出0,否则会因后面多输出空格而返回“格式错误”。

const int N=1e5+10;
struct Node
{
    char name[10];
    int year,month,day;
    bool operator<=(const Node &W) const
    {
        if(year == W.year)
        {
            if(month == W.month)
                return day<=W.day;
            else return month<=W.month;
        }
        else return year<=W.year;
    }
    bool operator>=(const Node &W) const
    {
        if(year == W.year)
        {
            if(month == W.month)
                return day>=W.day;
            else return month>=W.month;
        }
        else return year>=W.year;
    }
}a[N],l,r,youngest,oldest;
int n;

void init()
{
    l.year=1814,r.year=2014;
    l.month=r.month=9;
    l.day=r.day=6;
    youngest=l,oldest=r;
}

int main()
{
    init();

    cin>>n;

    int cnt=0;
    for(int i=0;i<n;i++)
    {
        scanf("%s %d/%d/%d",a[i].name,&a[i].year,&a[i].month,&a[i].day);
        if(a[i] >= l && a[i] <= r)
        {
            cnt++;
            if(a[i] <= oldest) oldest=a[i];
            if(a[i] >= youngest) youngest=a[i];
        }
    }

    if(cnt) cout<<cnt<<' '<<oldest.name<<' '<<youngest.name<<endl;
    else cout<<0<<endl;
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14398758.html