B Birthday Boy (注意细节ac题)

个人博客luckyboom.cn

9-23练习赛 2022年9月24日 Azhuo 0

题目大意:给你n个人的信息,让你找出一个时间,要求让你选择一天,使得这一天的前一个生日距离它最远,如果有多个一样的,有点选择10月27之后的。因为代码没注意细节if的判断出现bug导致一直WA,错误的核心代码我放在题解后


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.
Input
• 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.

Output
Print the fake birthday date (format: mm-dd) chosen by Bobby.
Sample Input 1 Sample Output 1
3
Henk 01-09
Roos 09-20
Pietje 11-11
09-19
Sample Input 2 Sample Output 2
16
Henk 01-09
Luc 12-31
Jan 03-22
Roos 09-20
Pietje 11-11
Anne 02-28
Pierre 09-25
Dan 12-15
Lieze 11-17
Charlotte 05-01
Lenny 08-02
Marc 04-25
Martha 06-12
John 03-26
Matthew 01-20
John 01-20
08-01
Sample Input 3 Sample Output 3
3
JohnIII 04-29
JohnVI 10-28
JohnIIX 04-28
04-27
Sample Input 4 Sample Output 4
3
CharlesII 04-30
CharlesV 10-29
CharlesVII 04-29
10-28

#include "bits/stdc++.h"
using namespace std;
int day(int n){//月份转化天数
    int sum=0;
    if(n>1)sum+=31;
    if(n>2)sum+=28;
    if(n>3)sum+=31;
    if(n>4)sum+=30;
    if(n>5)sum+=31;
    if(n>6)sum+=30;
    if(n>7)sum+=31;
    if(n>8)sum+=31;
    if(n>9)sum+=30;
    if(n>10)sum+=31;
    if(n>11)sum+=30;
    if(n>12)sum+=31;
    return sum;
}
int ansm=1,ansd;
void l(int n){//天数转化月份(错误核心在这里)
    int sum=n;
    if(sum>31)sum-=31,ansm++;
    if(ansm==2&&sum>28)sum-=28,ansm++;
    if(ansm==3&&sum>31)sum-=31,ansm++;
    if(ansm==4&&sum>30)sum-=30,ansm++;
    if(ansm==5&&sum>31)sum-=31,ansm++;
    if(ansm==6&&sum>30)sum-=30,ansm++;
    if(ansm==7&&sum>31)sum-=31,ansm++;
    if(ansm==8&&sum>31)sum-=31,ansm++;
    if(ansm==9&&sum>30)sum-=30,ansm++;
    if(ansm==10&&sum>31)sum-=31,ansm++;
    if(ansm==11&&sum>30)sum-=30,ansm++;
    if(ansm==12&&sum>31)sum-=31,ansm++;
    ansd=sum;
}
int ans[400];
int main(){
    int n;
    cin>>n;
    for (int i = 0; i < n; ++i) {
        string x;
        cin>>x;
        int a,b;
        scanf("%d-%d",&a,&b);
        int k=day(a)+b;
        ans[k]=1;
    }
    int a=301;
    int max=0;
    int no=0;
    for (int i = 300; i >= 0 ; --i) {//先逆推10.27前n天记录到no中
        if(ans[i]==0){
            no++;
        }
        else{
            break;
        }
    }
    for (int i = 301; i <= 700; ++i) {//模拟
        int index=i;
        if(index>365){
            index-=365;
        }
        if(ans[index]==0){
            no++;
            if(no>max){
                max=no;
                a=index;
            }
        }
        else {
            no=0;
        }
    }
    if(a>365){
        a-=365;
    }
    l(a);
    printf("%02d-%02d",ansm,ansd);


}
void l(int n){//31天时代码会if判断第二个语句导致错误
    int sum=n;
    if(sum>31)sum-=31,ansm++;
    if(sum>28)sum-=28,ansm++;
    if(sum>31)sum-=31,ansm++;
    if(sum>30)sum-=30,ansm++;
    if(sum>31)sum-=31,ansm++;
    if(sum>30)sum-=30,ansm++;
    if(sum>31)sum-=31,ansm++;
    if(sum>31)sum-=31,ansm++;
    if(sum>30)sum-=30,ansm++;
    if(sum>31)sum-=31,ansm++;
    if(sum>30)sum-=30,ansm++;
    if(sum>31)sum-=31,ansm++;
    ansd=sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值