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

一、第一题:日期差值

解题思路:模拟

写一个计算时间的板子两者相减

【Python程序代码】

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

二、第二题:日期问题

解题思路:枚举

把可能的日期枚举出来再判断去重排序

【Python程序代码】

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)

三、第三题:回文日期

解题思路:枚举

按年份枚举

【Python程序代码】

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)

四、第四题:回文日期

解题思路:枚举

按年份枚举

【Python程序代码】

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()

五、第五题:日期计算

解题思路:枚举

偷懒无脑用时间包

【Python程序代码】

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)
相关推荐
CodeDevMaster5 分钟前
browser-use:AI驱动的浏览器自动化工具使用指南
python·llm
程序员辣条7 分钟前
深度测评 RAG 应用评估框架:指标最全面的 RAGas
人工智能·程序员
curdcv_po8 分钟前
字节跳动Trae:一款革命性的免费AI编程工具完全评测
人工智能·trae
程序员辣条9 分钟前
为什么需要提示词工程?什么是提示词工程(prompt engineering)?为什么需要提示词工程?收藏我这一篇就够了!
人工智能·程序员·产品经理
孔令飞13 分钟前
Go:终于有了处理未定义字段的实用方案
人工智能·云原生·go
清流君26 分钟前
【MySQL】数据库 Navicat 可视化工具与 MySQL 命令行基本操作
数据库·人工智能·笔记·mysql·ue5·数字孪生
Blossom.11833 分钟前
人工智能在智能家居中的应用与发展
人工智能·深度学习·机器学习·智能家居·vr·虚拟现实·多模态融合
biter008835 分钟前
ubuntu(28):ubuntu系统多版本conda和多版本cuda共存
linux·人工智能·ubuntu·conda
内网渗透35 分钟前
Python 虚拟环境管理:venv 与 conda 的选择与配置
开发语言·python·conda·虚拟环境·venv
薄荷很无奈43 分钟前
CuML + Cudf (RAPIDS) 加速python数据分析脚本
python·机器学习·数据分析·gpu算力