2024蓝桥杯每日一题(时间日期)

一、第一题:日期差值

eb3010dca4c242999443d93f9461cd1c.png

解题思路:模拟
        写一个计算时间的板子两者相减

【Python程序代码】

mon = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def pd(x):
    if x%400==0 or (x%4==0 and x%100!=0):return True
    return False
def get_day(y,m,d):
    res = 0
    for i in range(1,y):
        if pd(i):res+=366
        else:res+=365
    if pd(y):mon[2]=29
    else:mon[2]=28
    for i in range(1,m):
        res += mon[i]
    return res+d
while True:
    try:
        a = input()
        b = input()
        if int(a)>int(b):a,b=b,a
        print(get_day(int(b[:4]), int(b[4:6]), int(b[6:]))-get_day(int(a[:4]), int(a[4:6]), int(a[6:]))+1)
    except:
        break

二、第二题:日期问题

cbff871365ec4946a34d1e15b9517695.png

ee629a796ef94084b40e681792c9842a.png

解题思路:枚举
        把可能的日期枚举出来再判断去重排序

【Python程序代码】

s = list(input().split('/'))
mon = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def pd(y):
    if y%400==0 or (y%4==0 and y%100!=0):return True
    return False
ans = []
ans.append(('20'+s[0],s[1],s[2]))
ans.append(('19'+s[0],s[1],s[2]))
ans.append(('20'+s[2],s[0],s[1]))
ans.append(('19'+s[2],s[0],s[1]))
ans.append(('20'+s[2],s[1],s[0]))
ans.append(('19'+s[2],s[1],s[0]))
res = []
for y,m,d in ans:
    if pd(int(y)):mon[2]=29
    else:mon[2]=28
    if 1960<=int(y)<=2059 and 1<=int(m)<=12 and 1<=int(d)<=mon[int(m)]:
        res.append((int(y+m+d),y,m,d))
res = list(set(res))
res.sort()
for a,y,m,d in res:
    print(y+'-'+m+'-'+d)

三、第三题:回文日期

37766fb6e3d749c6a98578b93a00ed2c.png

bbb431994476411996edd672fc13f3b5.png

解题思路:枚举
        按年份枚举

【Python程序代码】

mon = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def pd(y):
    if y%400==0 or (y%4==0 and y%100!=0):return True
    return False
def pd2(x):
    if pd(int(x[:4])):mon[2]=29
    else:mon[2]=28
    if 1000<=int(x[:4])<=9999 and 1<=int(x[4:6])<=12 and 1<=int(x[6:])<=mon[int(x[4:6])]:
        return True
    return False
a = input()
b = input()
res = 0
for i in range(int(a[:4]),int(b[:4])+1):
    if pd(i):mon[2]=29
    else:mon[2]=28
    tep1 = str(i)
    tep2 = reversed( list(tep1) )
    tep2 = ''.join(tep2)
    new_date = tep1+tep2
    if int(a) <= int(new_date) <= int(b):
        if pd2(new_date):res+=1
print(res)

 四、第四题:回文日期

 e92af67e994940ee882fbd02c8f6696f.png

5b3d930d691d42d1a7ad5a51bfd15a62.png

解题思路:枚举
        按年份枚举

【Python程序代码】

a = input()
mon = [0,31,28,31,30,31,30,31,31,30,31,30,31]
def pd(x):
    if x%400==0 or (x%4==0 and x%100!=0):return True
    return False
def pd2(x):
    if pd(int(x[:4])):mon[2]=29
    else:mon[2]=28
    if 1000<=int(x[:4])<=9999 and 1<=int(x[4:6])<=12 and 1<=int(x[6:])<=mon[int(x[4:6])]:return True
    return False
def work1():
    for i in range(int(a[:4]),10000):
        tep1 = str(i)
        tep2 = reversed(list(tep1))
        tep2 = ''.join(tep2)
        new_date = tep1 + tep2
        if pd2(new_date) and int(new_date)>int(a):
            print(new_date)
            return
def work2():
    for i in range(int(a[:4]),10000):
        tep1 = str(i)
        if tep1[0]==tep1[2] and tep1[1]==tep1[3] and tep1[0]!=tep1[1]:
            tep2 = reversed(list(tep1))
            tep2 = ''.join(tep2)
            new_date = tep1 + tep2
            if pd2(new_date) and int(new_date)>int(a):
                print(new_date)
                return
work1()
work2()

五、第五题:日期计算

08abdf4745744751bc5f557a58e02b36.png

115a20d1df1a4d93a831141ff7d7439d.png

解题思路:枚举
        偷懒无脑用时间包

【Python程序代码】

from datetime import *
a = input()
d = int(input())
st = datetime(int(a[:4]),1,1)
dt = timedelta(1)
for i in range(d-1):
    st+=dt
print(st.month)
print(st.day)

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学数学的懒哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值