蓝桥杯 日期问题


#include<cstdio>
#include<set>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
int monthday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int tran(int y){
    if(y>=60&&y<=99)y+=1900;
    else  y+=2000;
    return y;
}
string format(int a,int b,int c){
    a=tran(a);
    char s[15],t[15];
    sprintf(s,"%d-",a);
    sprintf(t,"%d-",b);
    if(b<10)strcat(s,"0");
    strcat(s,t);
    sprintf(t,"%d",c);
    if(c<10)strcat(s,"0");
    strcat(s,t);
    return s;
}
bool check(int a,int b,int c){
    a=tran(a);
    if(b<1||b>12)return false;
    //每4年一闰,100年不闰,400年又闰,3200年又不闰(闰年就是二月多一天)
    if((a%4==0&&a%100!=0)||a%400==0)monthday[2]=29;
    else monthday[2]=28;
    if(c<1||c>monthday[b])return false;
    return true;
}
int main(){
    string s;
    set<string> ans;
    cin>>s;
    //转换成数字好判断日期是否合法
    int a = (s[0]-'0')  *10+ (s[1]-'0');
    int b = (s[3] - '0') * 10 + (s[4] - '0');
    int c = (s[6] - '0') * 10 + (s[7] - '0');
    //按照年月日解析=======>a,b,c
    if(check(a,b,c))ans.insert(format(a,b,c));
    //按照月日年解析a,b,c=======>c,a,b
    if (check(c, a, b))  ans.insert(format(c,a,b));
    //按照日月年解析a,b,c=======>c,b,a
    if (check(c, b, a))  ans.insert(format(c, b,a));
    for(set<string>::iterator it=ans.begin();it!=ans.end();it++)
        cout<<*it<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值