Compute the Angle of the Hour and Minute Hand on a Clock
- 时间:2020-09-13 14:33:25
- 分类:网络文摘
- 阅读:145 次
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) —
推荐阅读:内容页关键词布局优化解析 中小企业需求在改变:SEO从业者需要顺应潮流 深度解析搜索引擎蜘蛛工作的原理 外贸网站建设不要忽视这6个网站设计操作 百度不再支持sitemapXML地图文档 站群推广的优点,SEO站群爆炸流量 谷歌外链用自动化工具发,真的靠谱吗 我的宝贝—《小学生之友》 写人作文娘是儿的天作文1200字 春日寻芳小学作文
- 评论列表
-
- 添加评论