假设我必须从 0-10 中选择一个数字。
我选的数字是 6。
我要选择的下一个数字是 0。
现在的规则是我必须继续将数字增加 1 或减少 1,数字也可以环绕最后一个数字。
现在最重要的是找到最短的方向。
所以
6-5-4-3-2-1-0 = 7 moves.
6-7-8-9-10-0 = 6 moves.
因此,在这种情况下,递增获胜。
好吧,我想出了这段代码(可能坏了)
int movesInc = 1;
int movesDec = 1;
int curNumber = 6;
int nextNumber = 0;
while((curNumber-- % 11) != nextNumber)
movesDec++;
while((curNumber++ % 11) != nextNumber)
movesInc++;
现在而不是在两个方向上使用while循环......并找出哪个需要更少的移动......
有什么方法可以在没有while循环的情况下做到这一点?也许只是某种数学方程式?