Birthday Boy(模拟)

题目描述
Bobby has just joined a new company, and human resources has asked him to note his birthday on the office calendar. Bobby the Birthday Boy wants to feel special! Also, Bobby the Birthday Boy does not mind lying for attention.
He notices that the longer people have not celebrated a birthday or eaten cake, the more they like it when a new one comes around. So he wants to pick his birthday in such a way that the longest period of time without a birthday possible has just passed. Of course he does not want to share his birthday with any colleague, either.
Can you help him make up a fake birthday to make him feel as special as possible? Bobby does not care about leap years: you can assume every year is not a leap year, and that no one has a birthday on the 29th of February. In case of a tie, Bobby decides to fill in the date that is soonest (strictly) after the current date, the 27th of October, because that means he will get to celebrate his birthday as soon as possible.
在这里插入图片描述
Figure 1: Sample case 2. Calendar is from http://printablecalendarholidays.com.

输入
• One line with a number 1 ≤ n ≤ 100, the number of colleagues Bobby has in his new office.
• Then follow n lines, each line corresponding to one coworker. Each line gives the name of the colleague (using at most 20 upper or lower case letters) separated from their birthday date by a space. The date is in format mm-dd.

输出
Print the fake birthday date (format: mm-dd) chosen by Bobby.

样例输入
3
Henk 01-09
Roos 09-20
Pietje 11-11

样例输出
09-19

思路
模拟找出最适合题意的生日日期

代码实现

#include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=105;
struct node
{
    int m;
    int d;
    int day;
} getdays[N];
string name;
int sto[N];
int a[N];
int month[14]= {31,28,31,30,31,30,31,31,30,31,30,31};
bool cmp(node a,node b)
{
    return a.day<b.day;
}
int getsum(int mon,int day)
{
    int ans=0;
    for(int i=0; i<mon-1; i++) ans+=month[i];
    ans+=day;
    return ans;
}
int main()
{
    int n;
    char u;
    scanf("%d",&n);
    if(n==1) 
    {
        cin>>name;
        cin>>getdays[1].m>>u>>getdays[1].d;
        int  i,sum=getsum(getdays[1].m,getdays[1].d)-1;
        for( i=0; i<12; i++)
        {
            if(sum-month[i]>=0) sum-=month[i];
            else break;
        }
        if(sum==0)
        {
            i--;
            if(i==-1) i=11;
            sum=month[i];
        }
        printf("%02d-%02d\n",i+1,sum);
        return 0;
    }
    for(int i=1; i<=n; i++)
    {
        cin>>name;
        cin>>getdays[i].m>>u>>getdays[i].d;
        getdays[i].day=getsum(getdays[i].m,getdays[i].d);
    }
    sort(getdays+1,getdays+n+1,cmp);
    sto[1]=365-getdays[n].day+getdays[1].day;
    for(int i=2; i<=n; i++) sto[i]=getdays[i].day-getdays[i-1].day;
    int maxx=0;
    for(int i=1; i<=n; i++) maxx=max(maxx,sto[i]);
    int cnt=0;
    for(int i=1; i<=n; i++) if(maxx==sto[i]) a[++cnt]=i;
    if(cnt==1)
    {
        int i,sum=sto[a[1]]+getdays[a[1]-1].day-1;
        if(a[1]==1) sum=getdays[1].day-1;
        sum=(sum+365)%365;
        for(i=0; i<12; i++)
        {
            if(sum-month[i]>=0) sum-=month[i];
            else break;
        }
        if(sum==0)
        {
            i--;
            if(i==-1) i=11;
            sum=month[i];
        }
        printf("%02d-%02d\n",i+1,sum);
    }
    else
    {
        int flag=0,minn1=inf,minn2=inf,pos;
        for(int i=1; i<=cnt; i++)
        {
            int tmp=sto[a[i]]+getdays[a[i]-1].day-1;
            if(tmp>365) tmp-=365;
            if(tmp>getsum(10,27))
            {
                if(tmp-getsum(10,27)<minn1)
                {
                    minn1=tmp-getsum(10,27);
                    flag=i;
                }
            }
            else
            {
                if((tmp-getsum(10,27))<minn2)
                {
                    minn2=tmp-getsum(10,27);
                    pos=i;
                }
            }
        }
        int i,sum;
        if(minn1!=inf)
        {
            i,sum=(sto[a[flag]]+getdays[a[flag]-1].day)-1;
            sum=(sum+365)%365;
            for( i=0; i<12; i++)
            {
                if(sum-month[i]>=0) sum-=month[i];
                else break;
            }
        }
        else
        {
            sum=getdays[pos].day-1;
            sum=(sum+365)%365;
            for( i=0; i<12; i++)
            {
                if(sum-month[i]>=0) sum-=month[i];
                else break;
            }
        }
        if(sum==0)
        {
            i--;
            if(i==-1) i=11;
            sum=month[i];
        }
        printf("%02d-%02d\n",i+1,sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值