因为存在跨天的可能性,所以计算两个时差再比较,比较不容易遗漏特殊情况:
def time_to_min(time_str):
"""将"hh:mm"格式的时间字符串转换为当天的分钟数"""
hour, minute = map(int, time_str.split(":"))
return hour * 60 + minute
t = int(input())
for _ in range(t):
# 读取每次查询的三个时间
t1 = time_to_min(input().strip())
t2 = time_to_min(input().strip())
t3 = time_to_min(input().strip())
# 计算预计花费时间(处理跨天情况:若为负数则加1440分钟)
expected_time = t2 - t1
if expected_time < 0:
expected_time += 1440
# 计算实际花费时间
actual_time = t3 - t1
if actual_time < 0:
actual_time += 1440
# 判断是否超时
print("Yes" if actual_time > expected_time else "No")
脑子比较灵光的,直接对t2,t3两个时间进行处理比较,风险较高,但也能做,选取下方代码作为示例:
t=int(input())
for _ in range(t):
t1=input()
t2h,t2=map(int,input().split(':'))
t3h,t3=map(int,input().split(':'))
if t3h<=22:
t3h+=24
if t2h<=22:
t2h+=24
if t2h<t3h:
print("Yes")
elif t2h==t3h and t2<t3:
print("Yes")
else :
print("No")



京公网安备 11010502036488号