日期问题

题目

小明正在整理一批历史文献。这些历史文献中出现了很多日期。

小明知道这些日期都在1960年1月1日至2059年12月31日。

令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。

更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

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

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入格式
一个日期,格式是”AA/BB/CC”。

即每个’/’隔开的部分由两个 0-9 之间的数字(不一定相同)组成。

输出格式
输出若干个不相同的日期,每个日期一行,格式是”yyyy-MM-dd”。

多个日期按从早到晚排列。

数据范围
0≤A,B,C≤9
输入样例:
02/03/04
输出样例:
2002-03-04
2004-02-03
2004-03-02

完整代码

#include <iostream>
#include <string>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;

int s[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int A, B, C;

typedef struct date{
    int y, m, d;
    date(){}
    date(int a, int b, int c) : y(a), m(b), d(c){}
    bool operator < (const date & other) const{
        if(y != other.y) return y < other.y;
        if(m != other.m) return m < other.m;
        return d < other.d;
    }
    bool operator == (const date & other) const{
        if(y == other.y && m == other.m && d == other.d)
            return true;
        return false;
    }
}date;

date res[3];
int index;

void check(int y, int m, int d){
    y += y <= 59 ? 2000 : 1900;
    int offset = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
    s[2] += offset;
    if(!(m >= 1 && m <=12)) return;
    if(!(s[m] >= d && d > 0)) return;
    //printf("%d-%02d-%02d\n",y, m, d);
    res[index++] = date(y, m, d);
    s[2] -= offset;
}

int main(){
    scanf("%d/%d/%d",&A, &B, &C);
    check(A, B, C);
    check(C, A, B);
    check(C, B, A);
    sort(res, res + index);
    printf("%d-%02d-%02d\n",res[0].y, res[0].m, res[0].d);
    for(int i = 1; i < index; i++){
        if(res[i] == res[i-1]) continue;
        else
            printf("%d-%02d-%02d\n",res[i].y, res[i].m, res[i].d);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值