466. 回文日期

摘要
Title: 466. 回文日期
Tag: 日期、回文串、闰年
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

466. 回文日期

  • 题意

    问两个日期之间存在多少个合法的回文日期

  • 思路

    日期问题模板解法

    • 先枚举所有要求日期是什么的集合
      比如这里说让日期为回文串,那么就先枚举所有8位数的回文串
    • 判断此串是否在范围内
      直接进行比较即可
    • 判断日期是否合法
      • 列出每个月的日期天数(2月按28天,平年列出)
      • 当月份不为2时,判断日期和日子是否合法
      • 当月份为2时,判断年份是否为闰年
        • 是4的倍数,不是100的倍数
        • 是400的倍数
  • 代码

    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
    '''
    Author: NEFU AB-IN
    Date: 2022-03-24 20:34:28
    FilePath: \ACM\Acwing\466.py
    LastEditTime: 2022-03-24 20:50:12
    '''
    months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


    #再判断日期是否合法
    def check(s):
    s = str(s)
    year = int(s[:4])
    month = int(s[4:6])
    day = int(s[6:])

    if month != 2:
    return 1 <= month <= 12 and 0 < day <= months[month]
    else:
    judge = (year % 100 and year % 4 == 0) or year % 400 == 0
    return day <= months[2] + judge


    start = input()
    end = input()

    ans = 0
    #先枚举所有的回文串
    for i in range(1000, 10000):
    s = str(i) + str(i)[::-1]
    #再判断是否在合法区间
    if start <= s <= end:
    ans += check(s)

    print(ans)
使用搜索:谷歌必应百度