【蓝桥杯】【真题】回文日期

题目描述

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

输入

输入包含一个八位整数N,表示日期。

输出

输出两行,每行1 个八位数。第一行表示下一个回文日期,第二行表示下
一个ABABBABA 型的回文日期。
 

样例输入

20200202

样例输出

20211202
21211212

一年最多有两个回文日期,检查回文是否符合要求

s =input().strip()
y=int(s[:4])
aflag =0
bflag =0
a=0
b=0
while True:
    temp = str(y)[::-1]
    m = int(temp[:2])
    d = int(temp[2:])
    if m ==2:
        if (y%4==0 and y%400!=0) or (y%400==0):
            if 1<=d<=29:
                if int(str(y)+temp)>int(s) and aflag==0:
                    a = int(str(y)+temp)
                    aflag =1
                if int(str(y)+temp)>int(s) and temp[0] == temp[2] and temp[1] == temp[3] and bflag==0:
                    b =  int(str(y)+temp)
                    bflag =1
        else:
            if 1<=d<=28:
                if int(str(y)+temp)>int(s) and aflag==0:
                    a = int(str(y)+temp)
                    aflag =1
                if int(str(y)+temp)>int(s) and temp[0] == temp[2] and temp[1] == temp[3] and bflag==0:
                    b =  int(str(y)+temp)
                    bflag =1
    elif m in [1,3,5,7,8,10,12]:
        if 1<=d<=31:
            if int(str(y)+temp)>int(s) and aflag==0:
                a = int(str(y)+temp)
                aflag =1
            if int(str(y)+temp)>int(s)  and temp[0] == temp[2] and temp[1] == temp[3] and bflag==0:
                b =  int(str(y)+temp)
                bflag =1
    elif m in [2,4,6,9,11]:
        if 1<=d<=30:
            if int(str(y)+temp)>int(s) and aflag==0:
                a = int(str(y)+temp)
                aflag =1
            if int(str(y)+temp)>int(s)  and temp[0] == temp[2] and temp[1] == temp[3] and bflag ==0:
                b =  int(str(y)+temp)
                bflag =1
    if aflag==1 and bflag==1:
        print(a)
        print(b)
        break
    y+=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值