置頂

GeoGebra 文章目錄

GeoGebra 文章目錄  更新日期:2018/2/8 我將 GeoGebra 相關的文章及檔案連結都整理在這篇裡,之後如果有新的文章也會同時更新這個目錄。上傳到 GeoGebraTube 的檔案,我有試著用 Google Chrome 63.0.3239.13...

熱門文章

2026年6月18日 星期四

LeetCode 解題筆記:1344. Angle Between Hands of a Clock

作者:王一哲
日期:2026年6月18日


LeetCode 題目連結:1344. Angle Between Hands of a Clock

解題想法


中等難度,ZeroJudge 上有一題幾乎一樣的題目 d095. 00579 - ClockHands。解題時分成 3 個步驟:
  1. 計算時針與 12 時的夾角 $a$,先將 hour 對 12 取餘數,每小時轉 30 度。
  2. 計算分針與 12 時的夾角 $b$,每分鐘轉 6 度。
  3. 計算 $a, b$ 差值的絕對值 $c$,如果 $c$ 是鈍角,換成從另一側算角度。
我的 Python 程式碼測試過 2 次,第 1 次跑 4 ms,第 2 次跑 0 ms,差異有點大。

Python 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 19.37 MB, beats 84.16%.
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        a = (hour % 12 + minutes / 60.0) * 30.0  # 12 時變為 0 時,每小時轉 30 度
        b = minutes * 6.0  # 每分鐘轉 6 度
        c = abs(a - b)  # 夾角
        if c > 180.0: c = 360.0 - c  # 如果是鈍角,換成從另一側算角度
        return c


C++ 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 7.97 MB, beats 95.61%.
class Solution {
public:
    double angleClock(int hour, int minutes) {
        double a = (hour % 12 + minutes / 60.0) * 30.0;  // 12 時變為 0 時,每小時轉 30 度
        double b = minutes * 6.0;  // 每分鐘轉 6 度
        double c = abs(a - b);  // 夾角
        if (c > 180.0) c = 360.0 - c;  // 如果是鈍角,換成從另一側算角度
        return c;
    }
};

沒有留言:

張貼留言