1229. 日期问题

摘要
Title: 1229. 日期问题
Tag: 日期
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

1229. 日期问题

  • 题意

    小明正在整理一批历史文献。这些历史文献中出现了很多日期。
    小明知道这些日期都在1960年1月1日至2059年12月31日。
    令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
    更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
    比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。
    给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

  • 思路

    正解思路应是

    • 先枚举start到end的日期
    • 判断日期是否合法
    • 判断是否符合输入的条件
      好处就是不用去重,也不用排序

    我这次的思路是

    • 先根据输入枚举出可能的日期,并凑成8位数
    • 判断是否在start到end之间
    • 判断日期是否合法
    • 最后再去重排序
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43

    '''
    Author: NEFU AB-IN
    Date: 2022-03-25 15:32:34
    FilePath: \ACM\Acwing\1229.py
    LastEditTime: 2022-03-25 15:32:34
    '''
    mouths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


    def check(year, mouth, day):
    year, mouth, day = map(int, [year, mouth, day])
    if mouth != 2:
    return 1 <= mouth <= 12 and 0 < day <= mouths[mouth]
    else:
    leap = year % 100 and year % 4 == 0 or year % 400 == 0
    return day <= mouths[2] + leap


    ans = []
    start = "19600101"
    end = "20591231"
    yy = ["19", "20"]
    a, b, c = input().split("/")
    #年/月/日
    for y in yy:
    year = y + a
    if check(year, b, c) and start <= (year + b + c) <= end:
    ans.append("-".join([year, b, c]))
    #月/日/年
    for y in yy:
    year = y + c
    if check(year, a, b) and start <= (year + a + b) <= end:
    ans.append("-".join([year, a, b]))
    #日/月/年
    for y in yy:
    year = y + c
    if check(year, b, a) and start <= (year + b + a) <= end:
    ans.append("-".join([year, b, a]))

    ans = sorted(list(set(ans)))
    for date in ans:
    print(date)
使用搜索:谷歌必应百度