日期问题

问题描述
  小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

  比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

  给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
输入格式
  一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9)
输出格式
  输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。
样例输入
02/03/04
样例输出
2002-03-04
2004-02-03
2004-03-02

#include<iostream>
#include<set>

using namespace std;

struct date{
    int y, m, d;
    bool operator <(const date &t) const{
        if(y != t.y) return y < t.y;
        if(m != t.m) return m < t.m;
        return d < t.d;
    }
};

set<date> v;

int check(date &t){
    if(t.y < 1960 || t.y > 2059) return 0;
    if(t.m > 12 || t.m < 1) return 0;
    if(t.d < 1) return 0;
    if((t.y % 4 == 0 && t.y % 100) || t.y % 400 == 0)
        if(t.m == 2) return t.d <= 29;
        
    if(t.m == 2) return t.d <= 28;
    if(t.m == 4 || t.m == 6 || t.m == 9 || t.m == 11) return t.d <= 30;
    return t.d <= 31;
}

date get(int a, int b, int c, int m, int k){
    if(m == 0) return {k * 100 + a, b, c};
    if(m == 1) return {k * 100 + c, a, b};
    return {k * 100 + c, b, a};
}

int main(){
    int a, b, c;
    
    scanf("%d/%d/%d", &a, &b, &c);
    for(int i = 0; i < 3; i ++){
        date t1 = get(a, b, c, i, 19), t2 = get(a, b, c ,i, 20);
        if(check(t1)) v.insert(t1);
        if(check(t2)) v.insert(t2);
    }
    
    set<date> :: iterator iter = v.begin();
    while(iter!= v.end()){
        printf("%04d-%02d-%02d\n", (*iter).y, (*iter).m, (*iter).d);
        iter ++;
    }
    
    return 0;
}
posted @ 2020-09-02 16:30  yys_c  阅读(155)  评论(0编辑  收藏  举报