2023首届大学生算法大赛 - 日期格式


此题应该是本场比赛的签到题,一道究极无敌模拟题。

做出这道题需要拥有以下前置知识点:

1、闰年能被4整除,但是不能被100整除,或者能被400整除。

2、1~12月各有多少天,闰年2月有29天。

AC代码

#include <bits/stdc++.h>
using namespace std;

string s;
int first,second,third;
int md[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int rnmd[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
bool rn(int year){
    return ((year%4==0 and year%100!=0) or year%400==0);
}
bool MMDDYY(int year,int month,int day){
    if(rn(year)){//是闰年
        if(month>=1 and month<=12 and day>=1 and day<=rnmd[month] and year>=0 and year<=99)return true;
    }else{
        if(month>=1 and month<=12 and day>=1 and day<=md[month] and year>=0 and year<=99)return true;
    }
    return false;
}
bool DDMMYY(int year,int month,int day){
    if(rn(year)){//是闰年
        if(month>=1 and month<=12 and day>=1 and day<=rnmd[month] and year>=0 and year<=99)return true;
    }else{
        if(month>=1 and month<=12 and day>=1 and day<=md[month] and year>=0 and year<=99)return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);

    cin>>s;
    first=(s[0]-'0')*10+(s[1]-'0');
    second=(s[3]-'0')*10+(s[4]-'0');
    third=(s[6]-'0')*10+(s[7]-'0');
    if(MMDDYY(third,first,second) and DDMMYY(third,second,first)){
        cout<<"BOTH";
        return 0;
    }
    if(MMDDYY(third,first,second)){
        cout<<"MMDDYY";
        return 0;
    }
    if(DDMMYY(third,second,first)){
        cout<<"DDMMYY";
        return 0;
    }
    cout<<"NOTHING";

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值