蓝桥杯_PREV-39_日期问题_2th_in2021

由于审核时,大段题目 文字和其他文章题目内容 重复,导致审核不过,所以这里题目略:

AC代码:

#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

string date;
string AA,BB,CC;
int aa,bb,cc;
vector<string> Dates;

int StoI(string st){
    stringstream ss(st);
    int t;
    ss>>t;
    return t;
}

string ItoS(int t){
    stringstream is;
    is<<t;
    return is.str();
}

void getAABBCC(){
    AA=date.substr(0,2);//从下标2开始的2个子串
    BB=date.substr(3,2);
    CC=date.substr(6,2);
    aa=StoI(AA);
    bb=StoI(BB);
    cc=StoI(CC);
}
/*
    这些日期都在1960年1月1日至2059年12月31日
*/
int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年
int common[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年

bool isLeap(int year){
    if( (year%4==0 && year%100!=0) || year%400==0 )
        return true;
    return false;
}

void getExactDate(int year,int month,int day){
    string res="error";
    if( (isLeap(year) && day<=leap[month-1]) ||
       (!isLeap(year) && day<=common[month-1]) )
        res=ItoS(year)+"-"+((month<10)?("0"):(""))+ItoS(month)+"-"+
        ((day<10)?("0"):(""))+ItoS(day);

    if(res!="error" && find(Dates.begin(),Dates.end(),res)==Dates.end() )//日期正确且未重复
        Dates.push_back(res);
}

void isYYMMDD(){
    if(BB>"00" && BB<="12" && CC>"00" && CC<="31"){
        int year;
        if(AA<="59")
            year=StoI("20"+AA);
        else
            year=StoI("19"+AA);
        getExactDate(year,bb,cc);
    }
}

//AA BB CC
void isMMDDYY(){
    if(AA>"00" && BB>"00" && AA<="12" && BB<="31"){
        int year;
        if(CC<="59")
            year=StoI("20"+CC);
        else
            year=StoI("19"+CC);
        getExactDate(year,aa,bb);
    }
}

// AA BB CC
void isDDMMYY(){
    if(BB>"00" && AA>"00" && BB<="12" && AA<="31"){
        int year;
        if(CC<="59")
            year=StoI("20"+CC);
        else
            year=StoI("19"+CC);
        getExactDate(year,bb,aa);
    }
}

int main()
{
    cin>>date;
    getAABBCC();
    isYYMMDD();
    isMMDDYY();
    isDDMMYY();
    sort(Dates.begin(),Dates.end());

    for(int i=0;i<Dates.size();i++)
        cout<<Dates[i]<<endl;

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值