Compute the Angle of the Hour and Minute Hand on a Clock
- 时间:2020-09-13 14:33:25
- 分类:网络文摘
- 阅读:92 次
Given a time in the format of hour and minute, calculate the angle of the hour and minute hand on a clock.

Clock 3:30
This was recently asked by Microsoft. This is not a difficult question. We want to make sure that:
- The time could be in the format 24-hour. For example, 15:30 is the same as 3:30.
- The hour minute also moves slightly (portion) when minute hand moves.
- One minute difference has a 6 rad degree – 60 minutes is 360 degree.
Thus, the following Python code should be straigtforward with O(1) constant in both time and space – just pure math calculations.
1 2 3 4 5 6 7 8 9 10 11 | def calcAngle(h, m): # hour hand gets percentage angle h = (h + (m / 60.0)) * 5 % 60 diff = abs(h - m) return int(diff * 6) if __name__ == "__main__": print(calcAngle(3, 30)) # 75 print(calcAngle(12, 30)) # 165 |
def calcAngle(h, m): # hour hand gets percentage angle h = (h + (m / 60.0)) * 5 % 60 diff = abs(h - m) return int(diff * 6) if __name__ == "__main__": print(calcAngle(3, 30)) # 75 print(calcAngle(12, 30)) # 165
A slightly different solution in C/C++ can be found here: C/C++ Program to Compute the Angle Between Hands of a Clock
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:回顾历史,自强不息作文500字 儿童节游必胜客作文 生活 感恩 由误解所想到的作文450字 我的家乡周口作文 三下乡中的失误和反省 六年级感恩父亲节作文1000字 手工纸剪小树数学题:用一张长45cm、宽21cm的手工纸,能剪几棵这样的小树? 数学题:两人轮流报数,每次只能报1或2,把两人报的所有数加起来 数学题:下面三位同学要去量身高、验视力,每项检查都要3分钟
- 评论列表
-
- 添加评论