0
点赞
收藏
分享

微信扫一扫

【思特奇杯·云上蓝桥-算法集训营】第1周 09.日期问题(python)

ixiaoyang8 2022-01-07 阅读 19

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

答案提交

while True:
    try:
        a = list(map(int, input().split("/")))
        res = []


        def isRN(year):
            if year % 4 == 0 and year % 100 != 0:
                return True
            elif year % 400 == 0:
                return True
            else:
                return False


        def f(x, y, z):  # 年 月 日
            if x >= 0 and x <= 59:
                x += 2000
            elif x >= 60 and x <= 99:
                x += 1900
            if y <= 0 or y > 12:
                return False
            if z <= 0 or z > 31:
                return False

            if isRN(x) and y == 2 and z > 29:
                return False
            if isRN(x) == False and y == 2 and z > 28:
                return False
            if y == 4 and z > 30:
                return False
            if y == 6 and z > 30:
                return False
            if y == 9 and z > 30:
                return False
            if y == 11 and z > 30:
                return False
            else:
                if y < 10:
                    y = str(0) + str(y)
                if z < 10:
                    z = str(0) + str(z)
                res.append(str(x) + "-" + str(y) + '-' + str(z))
                return


        f(a[0], a[1], a[2])
        f(a[2], a[0], a[1])
        f(a[2], a[1], a[0])
        for i in sorted(list(set(res))):
            print(i)

    except:
        break


举报

相关推荐

0 条评论