GYM 101142 K.King’s Heir(水~)

本文介绍了一个简单的算法,用于从国王的成年儿子中选择最年长者作为新国王。通过输入国王去世日期及各王子的出生日期,筛选出年龄大于等于18岁的王子,并按年龄大小进行排序,选取最年长者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

国王去世了,要选出其儿子中满18岁中最大的儿子当新的国王,给出国王去世日期和其n个儿子的出生日期,求新国王的编号

Input

第一行输入三个整数D,M,Y表示国王在YMD日去世的,之后输入一整数n表示国王儿子数量,之后n行每行输入三个整数di,mi,yi表示第i个儿子是dimidi日出生的(1D,di31,1M,mi12,1Y,yi9999,1n100),每年二月都是28天,没有两个儿子是同一天生的

Output

输出当新国王的儿子编号,如果没有满足条件的儿子则输出1

Sample Input

22 10 2016
7
28 2 1999
22 7 1995
21 10 1998
23 10 1998
3 9 2000
1 4 2013
17 12 2004

Sample Output

3

Solution

简单题,把年满十八周岁的儿子拿出来排个序即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=105;
struct node
{
    int d,m,y,id;
    bool operator<(const node &b)const
    {
        if(y!=b.y)return y>b.y;
        if(m!=b.m)return m>b.m;
        return d>b.d;
    }
}a[maxn],b,c;
int n;
int main()
{
    freopen("king.in","r",stdin);
    freopen("king.out","w",stdout);
    while(~scanf("%d%d%d",&b.d,&b.m,&b.y))
    {
        scanf("%d",&n);
        int m=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&c.d,&c.m,&c.y);
            c.id=i;
            if(b.y-c.y>18||b.y-c.y==18&&(b.m>c.m||b.m==c.m&&b.d>=c.d))a[m++]=c;
        }
        if(m==0)printf("-1\n");
        else 
        {
            sort(a,a+m);
            printf("%d\n",a[0].id);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值