题解 | 回文日期

回文日期

https://www.nowcoder.com/practice/0372242deac541d0b578cc6563395681

a = input()
b = input()

ya, yb = a[:4], b[:4]
d31 = ["01", "03", "05", "07", "08", "10", "12"]
d30 = ["04", "06", "09", "11"]

ans = 0
for i in range(int(ya), int(yb) + 1):
    md = str(i)[::-1]
    m, d = md[:2], md[2:4]  # 不可能出现负数
# 查看月份是否正确
    if int(m) > 12:
        continue
#查看日期(大小月,闰年月),是否正确
    if m == "02":  # 二月份
        if (i % 4 == 0 and i % 100 != 0 or i % 400 == 0) and int(d) <= 29:  # 闰年二月份,但日期大于29
            ans += 1
        elif int(d) <= 28:  # 平年二月份,但日期大于28
            ans += 1
    elif m in d31 and int(d) <= 31:  # 大月,但日期大于31
        ans += 1
    elif m in d30 and int(d) <= 30:  # 小月,但日期大于30
        ans += 1

print(ans)

全部评论

相关推荐

yubullym:双非目前 0 正式 offer,打算继续实习到 1 月准备春招了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务