#29245: Python 3行解


a0962014248gg@gmail.com (李福昌)

School : 高雄市立前鎮高級中學
ID : 146183
IP address : [114.40.174.87]
Last Login :
2022-08-02 04:50:25
d669. 11677 - Alarm Clock -- UVa11677 | From: [114.40.150.8] | Post Date : 2022-02-08 20:06

Python(3.8+) 3行解:

while(sum(inp := [int(_) for _ in input().split()])):
    t1, t2 = inp[0]*60 + [1], inp[2]*60 + [3]
    print(60*24 - t1 + t2 if(t1 > t2) else t2 - t1)

Python AC解:

while(True):
    inp = [int(_) for _ in input().split()]
    if(not sum(inp)):break
    t1, t2 = inp[0]*60 + [1], inp[2]*60 + [3]
    print(60*24 - t1 + t2 if(t1 > t2) else t2 - t1)

Python 解題觀念:
1. 利用推導式取得輸入
2. 如何以 not h1+m1+h2+m2 來判斷是否結束迴圈

解題思路:
1. 取得輸入
2. 全部轉為單位分鐘t => (t = h*60 + m)
3-1. 如果t1 > t2, 則答案為 一天有幾分鐘-t1+t2 => (ans = 60*24 - t1 + t2)
3-2. 反之則為 t2 - t1

 
ZeroJudge Forum