第九届蓝桥杯---航班时间python

时间:2024-04-01 11:40:30

1.总结:
时 = 秒数//3600
分 = 秒数%3600//60
秒= 秒数%60

print(‘{:02d}:{:02d}:{:02d}’.format(res//3600, res%3600//60, res%60))
{:02d}表示输入的整数是两位不足前面补0,d表示整数

# 时间计算函数
def time_caculate(t):
    m1 = t[0].split(":")
    m2 = t[1].split(":")
    # 如果首位数字是0不能进行乘法操作,比如05要转换为5
    if m1[0][0] == "0":
        m1[0] = m1[0][1]
    if m1[1][0] == "0":
        m1[1] = m1[1][1]
    if m1[2][0] == "0":
        m1[2] = m1[2][1]
        
    if m2[0][0] == "0":
        m2[0] = m2[0][1]
    if m2[1][0] == "0":
        m2[1] = m2[1][1]
    if m2[2][0] == "0":
        m2[2] = m2[2][1]
    
    stamp1 = 3600 * int(m1[0]) + 60 * int(m1[1]) + int(m1[2])
    stamp2 = 3600 * int(m2[0]) + 60 * int(m2[1]) + int(m2[2])
    
    if len(t) == 3:
        stamp2 += int(t[2][2]) * 3600 * 24
    # 返回时间戳
    return stamp2 - stamp1
    
        
# 输入数据组数
T = int(input())

# 键盘录入组数,打印结果
for _ in range(T):
    t1 = str(input()).split(" ")
    t2 = str(input()).split(" ")
    # 计算t1和t2的时间差
    res1 = time_caculate(t1)
    res2 = time_caculate(t2)
    res = int((res1 + res2) / 2)
    print('{:02d}:{:02d}:{:02d}'.format(res//3600, res%3600//60, res%60))

题目链接