改动于这篇文章(18条消息) python回文日期蓝桥杯解题思路_一台电脑走天下的博客-CSDN博客_回文日期python
他只能90分,因为每天每天的判断,会很慢。其实一年只需要判断一次就好。
#回文日期
import datetime
import calendar
# calendar.monthrange(2013,12)#返回指定 年份 的指定 月份 的第一天是星期几和这个月的天数。
N=input()
year,mm,day=eval(N[:4]),N[4:6],N[6:] #年月日
huiwen=[] #保存第一种回文日期类型
AB=[] #保存ABABBABA型回文日期
k = 0
while len(huiwen)*len(AB)==0:
if k==0:#判断本年的
years = str(year)[4::-1]
if int(years[0:2])<=12 and int(years[0:2])>=int(mm):#月份判断,是否满足mm到12月
if int(years[0:2])>int(mm) or (int(years[0:2])==int(mm) and int(years[2:4])>int(day)):#判断日期是否在当前日期之后
if int(years[2:4])<=calendar.monthrange(int(year),int(years[0:2]))[1]:#回文判断
huiwen.append(str(year)+str(years))
if str(years)[0] == str(years)[2] and str(years)[1] == str(years)[3]:
AB.append(str(year)+str(years))
k = k+1
continue
#判断以后年的
year += 1
years = str(year)[4::-1]
if int(years[0:2])<=12 and int(years[0:2])>0:
if int(years[2:4])<=calendar.monthrange(int(year),int(years[0:2]))[1]:
huiwen.append(str(year)+str(years))
if str(years)[0] == str(years)[2] and str(years)[1] == str(years)[3]:
AB.append(str(year)+str(years))
print(huiwen[0])
print(AB[0])