(python解题)2020蓝桥杯回文日期

2020年春节期间,有一个特殊的日期引起了大家的注意: 2020年2 月2日。因为如果将这个日期按“yyymmdd"的格式写成一一个8位数是20200202恰好是一个回文数。我们称这样的日期是回文日期。
有人表示20200202是“千年一遇” 的特殊日子。对此小明很不认同,因为不到2年之后就是下一个回文日期: 20211202 即2021年12月2日。也有人表示20200202并不仅仅是一个回文日期,还是一个ABABBABA型的回文日期。对此小明也不认同,因为大约100 年后就能遇到下一个ABABBABA型的回文日期: 21211212 即2121年12月12日。算不上“千年一遇”,项多算“千年两遇”。给定一个8位数的日期,请你计算该日期之后下一一个回文日期和下一个ABABBABA型的回文日期各是哪一天。

from calendar import isleap

gap_year = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
normal_year = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def judge(date, is_gap):
    month = int(date[:2])
    day = int(date[2:])
    if month > 12:
        return False
    if is_gap:
        return day < gap_year[month]
    else:
        return day < normal_year[month]


def judge_next_year(year: str):
    res_year = year[::-1]
    new_year = year + res_year
    revers_str = new_year[::-1]
    if new_year == revers_str:
        is_gap = isleap(int(year))
        flag = judge(res_year, is_gap)
        if flag:
            return [True, revers_str]
        else:
            return [False, revers_str]
    else:
        return [False, revers_str]


def get_next(data: str):
    year = data[:4]
    res = list()
    while True:
        year = str(int(year) + 1)
        ans = judge_next_year(year)
        flag, tmp = ans[0], ans[1]
        if flag:
            res.append(tmp)
            break
    num = int(data[0:2])
    while True:
        num += 1
        year = str(num) + str(num)
        ans = judge_next_year(year)
        flag, tmp = ans[0], ans[1]
        if flag:
            res.append(tmp)
            break
    return res
    
if __name__ == '__main__':
    data = input()
    res = get_next(data)
    for i in res:
        print(i)

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值