Blue Bridge Cup Python Group - Palindromic Dates
Topic description
During the 2020 Chinese New Year, a special date caught everyone's attention: February 2, 2020. Because if this date is written in the format of "yyyymmdd" as an 8-digit number, it is 20200202, which happens to be a palindrome. We call such dates a palindrome date.
Some people say that 20200202 is a special day "once in a thousand years". Xiao Ming disagrees with this very much, because less than 2 years later will be the next palindrome date: 20211202, which is December 2, 2021.
Some people also said that 20200202 is not just a palindrome date, but a palindrome date of ABABABA type. Xiao Ming also disagrees with this, because in about 100 years, the next palindrome date of the ABABABA type will be encountered: 21211212, which is December 12, 2121. It's not "once in a thousand years", but at most "twice in a thousand years".
Given an 8-digit date, please calculate which day is the next palindrome date and the next palindrome date of type ABABABA after the date.
enter description
The input contains an eight-digit integer NN representing the date.
For all evaluation cases, 10000101 \leq N \leq 8999123110000101≤N≤89991231 guarantees that NN is an 8-digit representation of a valid date.
output description
Output two lines, each with 1 octet. The first row represents the date of the next palindrome, and the second row represents the date of the next palindrome of type ABAABBABA.
import os
import sys
import datetime #导入日期库
# 请在此输入您的代码
idate=input()
y=int(idate[:4])#取出输入的年月日
m=int(idate[4:6])
d=int(idate[6:])
dd=datetime.date(y,m,d)#将输入的表示日期的字符串转换成日期
flag=True #回文日期只输出一次
for n in range(9999999):
dd=dd+datetime.timedelta(days=1)#日期不断增加1天
sd=str(dd).replace('-','')#将日期中的-去掉
if sd[:]==sd[::-1]:#判断日期是否是回文
if flag:
print(int(sd))#输出回文日期
flag=False#下次不输出回文日期
if sd[0]==sd[2]==sd[5]==sd[7] and sd[1]==sd[3]==sd[4]==sd[6]: #判断是否是ABABBABA类型
print(int(sd))#输出
break#结束循环
Thank you for your support. Your one-click three-connection is the biggest driving force for Ganggang students!