模拟时钟的最短路径算法 [英] Shortest path algorithm for an analog clock
问题描述
我遇到了一个家庭作业问题,如果有人可以帮助我指出正确的方向,那将是非常有用的。
I've got a c homework problem that is doing my head in and will be greatful if anyone can help point me in the right direction.
如果我有两个在诸如t1(55分钟)和t2(7分钟)之类的模拟手表上,您需要计算两分钟之间的最短步数。
If I have two minutes points on an analog watch such as t1 (55 minutes) and t2 (7 minutes), I need to calculate the shortest amount of steps between the two points.
什么到目前为止,我想出了以下两个等式:
What I've come up with so far is these two equations:
-t1 + t2 + 60 =
-55 + 7 + 60
= 12
t1 - t2 + 60 =
55 - 7 + 60
= 108
12 is lower then 108, therefore 12 steps is the shortest distance.
如果我比较两个结果并使用最低的结果,这似乎很好用。但是,如果我选择另外两个点,例如让t1 = 39和t2 = 34并将它们插入等式:
This appears to work fine if I compare the two results and use the lowest. However, if I pick out another two points for example let t1 = 39 and t2 = 34 and plug them into the equation:
-t1 + t2 + 60 = -39 + 34 + 60 = 55
t1 - t2 + 60 = 39 - 34 + 60 = 35
35 is lower then 55, therefore 35 steps is the shortest distance.
但是,35不是正确的答案。 5步是最远的距离(39-34 = 5)。
However, 35 isn't the correct answer. 5 steps is the shorest distance (39 - 34 = 5).
我的大脑有点炸了,我知道我缺少一些简单的东西。谁能帮忙?
My brain is a little fried, and I know I am missing something simple. Can anyone help?
推荐答案
您想要的是60的加法和减法。查看%
操作员。确保正确处理负片。
What you want is addition and subtraction modulo 60. Check out the %
operator. Make sure you handle negatives correctly.
这篇关于模拟时钟的最短路径算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!