0
点赞
收藏
分享

微信扫一扫

日期问题 (20分)


小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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

//
// Created by TIGA_HUANG on 2020/10/6.
//

#include <iostream>
#include <sstream>
#include <set>

using namespace std;

int mm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

string int_string(int x) {
    string str;
    stringstream ss;
    ss << x;
    ss >> str;
    if (str.length() < 2)
        str = '0' + str;
    return str;
}

string check(int y, int m, int d) {
    if (y < 0 || y >= 100)
        return "";
    if (y >= 60 && y < 100)
        y += 1900;
    else
        y += 2000;
    if (m <= 0 || m > 12)
        return "";
    if (m == 2) {
        if ((y % 4 == 0 && y % 100) || y % 400 == 0) {
            if (d > 29 || d <= 0)
                return "";
        } else {
            if (d > 28 || d <= 0)
                return "";
        }
    } else {
        if (d > mm[m - 1] || d <= 0)
            return "";
    }
    return int_string(y) + '-' + int_string(m) + '-' + int_string(d);
}

int main() {
    int y, m, d;
    scanf("%d/%d/%d", &y, &m, &d);
    string ans;
    set<string> s;
    ans = check(y, m, d);
    if (!ans.empty())
        s.insert(ans);
    ans = check(d, y, m);
    if (!ans.empty())
        s.insert(ans);
    ans = check(d, m, y);
    if (!ans.empty())
        s.insert(ans);
    for (set<string>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << '\n';
    }
    return 0;
}


举报

相关推荐

0 条评论