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;
}

猜你喜欢

转载自blog.csdn.net/qq_17807067/article/details/129940530