Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 模拟时钟的最短路径算法_Algorithm_Math - Fatal编程技术网

Algorithm 模拟时钟的最短路径算法

Algorithm 模拟时钟的最短路径算法,algorithm,math,Algorithm,Math,我有一个c级的家庭作业问题,这正困扰着我,如果有人能帮我指出正确的方向,我将非常感激 如果模拟手表上有两个分钟点,比如t1(55分钟)和t2(7分钟),我需要计算两点之间的最短步长 到目前为止,我得出了以下两个方程式: -t1 + t2 + 60 = -55 + 7 + 60 = 12 t1 - t2 + 60 = 55 - 7 + 60 = 108 12 is lower then 108, therefore 12 steps is the shor

我有一个c级的家庭作业问题,这正困扰着我,如果有人能帮我指出正确的方向,我将非常感激

如果模拟手表上有两个分钟点,比如t1(55分钟)和t2(7分钟),我需要计算两点之间的最短步长

到目前为止,我得出了以下两个方程式:

-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,并将它们插入方程中:

-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)

我的大脑有点紧张,我知道我错过了一些简单的事情。有人能帮忙吗


你想要的是模60的加减法。查看
%
操作符。确保正确处理底片。

如果不想使用%operator,请尝试这样想: 对于每两个点(t1;t2),您将有两种方式来连接它们:一条路径将穿过0(12点钟),另一条不会

假设t2>=t1,第二个距离很容易计算:它是t2-t1。 另一个距离是t1+60-t2


我认为你的错误是在第一个表达式中加了60。

+1,因为你提出了一个明确的问题,并且事先透露这是家庭作业。:)
t1-t2+60=39-34+60=35不正确:结果应该是
65
。无论如何,Edward Z.Yang回答了这个问题。谢谢Edward,让我现在能够解决这个问题并得到正确的结果!