栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

蓝桥杯——历届试题 回文日期-python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝桥杯——历届试题 回文日期-python

思路:每年都只有一个回文串例如1234年 就是12344321,所以根据前4位年份去构建对应的回文串,判断月份、日份是否在合法范围内即可。ABABBABA型同理只有一个,可以根据前2位构建这种类型的回文串。

n=int(input())+1
n1=n
#用字典存储每个月对应的天数   xx为闰年时2月的天数
dict={"01":31,"02":28,"03":31,"04":30,"05":31,"06":30,"07":31,"08":31,"09":30,"10":31,"11":30,"12":31,"xx":29}
while(1):
    s1=str(n)
    s2=str(int(n/10000))+s1[3]+s1[2]+s1[1]+s1[0]        #构建最近的回文数
    temp=s2[4]+s2[5]        #计算月份
    year=int(s2[0]+s2[1]+s2[2]+s2[3])
    if ((year%4==0 and year%100!=0) or year%400==0) and temp=="02":   #将闰年的2月月份改为xx
        temp="xx"
    if temp not in dict.keys():    #判断月份是否在字典中
        n+=10000
        continue
    else:
        if dict.get(temp)s1[0]):    #如果初始为12340123   则直接构建的话  应该是12211221<12340123  所以提前把第二位+1 13311331符合AB型条件
    n1 += 1000000
while(1):
    s1 = str(n1)
    s2 = str(int(n1 / 1000000)) + s1[0] + s1[1] + s1[1] + s1[0] + s1[1] + s1[0]
    temp = s2[4] + s2[5]
    year = int(s2[0] + s2[1] + s2[2] + s2[3])
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        temp = "xx"
    if temp not in dict.keys():
        n1 += 1000000
        continue
    else:
        if dict.get(temp) < int(s2[6] + s2[7]):
            n1 += 1000000
            continue
        else:
            print(s2)
            break
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号