天天看點

藍橋杯 曆屆試題 日期問題python

資源限制

時間限制:1.0s 記憶體限制:256.0MB

問題描述

  小明正在整理一批曆史文獻。這些曆史文獻中出現了很多日期。小明知道這些日期都在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

資料規模和約定

  峰值記憶體消耗(含虛拟機) < 256M

  CPU消耗 < 1000ms

按照題目意思一步一步寫,最後要排個序,去個重。

AC代碼

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

           

繼續閱讀