我有两个以 16 为模的循环整数,因此它们的值介于 0 到 15 之间。
我需要比较两个数字以确定是否n_1
大于n_0
n_1 > n_0
显然,这没有准确定义,所以我定义n_1
大于n_0
如果小于前面8个“数字”,否则小于n_0
(如果不相等)。
I.e. if:
n_0 = 0
if n_1 is between 1 and 8 (both inclusive)
then n_1 is greater than n_0.
n_0 = 5
if n_1 is between 6 and 15 (both inclusive)
then n_1 is greater than n_0.
n_0 = 12
if n_1 is between 13 and 15 (both inclusive)
or between 0 and 4 (both inclusive)
then n_1 is greater than n_0.
我如何以编程方式表达这种比较?
我确信我对上面的术语感到困惑,所以请随时纠正我的措辞。 :)
我正在考虑一个有 16 个小时的时钟。这个想法基本上是将 n0 移动到 0 位置,并将 n1 移动相同数量的“刻度”。现在您可以简单地检查 n1 是更大还是更小,具体取决于它是在 8 点之前还是 8 点之后。
public int compare (int n0, int n1){
int ticksToZero = 16 - n0;
if(n0 == n1)
return 0;
else if((n1 + ticksToZero) % 16 <= 8)
return -1; //n0 is smaller than n1
else
return 1; //n0 is larger than n1
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)