LeetCode 1344. 时钟指针的夹角

Table of Contents

中文版:

英文版:

My answer:

解题报告:


中文版:

给你两个数 hour 和 minutes 。请你返回在时钟上,由给定时间的时针和分针组成的较小角的角度(60 单位制)。

示例 1:

输入:hour = 12, minutes = 30
输出:165

示例 2:

输入:hour = 3, minutes = 30
输出;75

示例 3:

输入:hour = 3, minutes = 15
输出:7.5

示例 4:

输入:hour = 4, minutes = 50
输出:155

示例 5:

输入:hour = 12, minutes = 0
输出:0
 

提示:

1 <= hour <= 12
0 <= minutes <= 59
与标准答案误差在 10^-5 以内的结果都被视为正确结果。

英文版:

Given two numbers, hour and minutes. Return the smaller angle (in sexagesimal units) formed between the hour and the minute hand.

Example 1:

Input: hour = 12, minutes = 30
Output: 165

Example 2:

Input: hour = 3, minutes = 30
Output: 75

Example 3:

Input: hour = 3, minutes = 15
Output: 7.5

Example 4:

Input: hour = 4, minutes = 50
Output: 155

Example 5:

Input: hour = 12, minutes = 0
Output: 0
 

Constraints:

1 <= hour <= 12
0 <= minutes <= 59
Answers within 10^-5 of the actual value will be accepted as correct.

My answer:

import math
class Solution:
    def angleClock(self, hour: int, minutes: int) -> float:
        res = 0
        m = minutes * 6   # 分针的度数
        if hour == 12:
            h = 0  # 特判:如果为0点,则为0°
        h = (hour + minutes / 60) * 360/12  # 时针的度数
        res = abs(m - h)
        if res > 180:
            res = 360 - res
        return res
        

解题报告:

本题其实是一道模拟题,只要懂得基本数学知识即可。

假设零点为 0 度,求出分针的度数(一分钟是 6 度),时针的度数(一小时是30度,且随着分针不同时针也会走,所以是 hour + minutes / 60)。之后求夹角即为两个度数之差。此事要注意的是,题目要求求锐角,所以需要特判,如果结果 res > 180,则要 360° - res。

发布了112 篇原创文章 · 获赞 41 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u011675334/article/details/104237851