Algorithm 检查循环(模16)数是否大于另一个?

Algorithm 检查循环(模16)数是否大于另一个?,algorithm,math,cycle,modulus,Algorithm,Math,Cycle,Modulus,我有两个循环整数,模16,所以它们假设值在0到15之间 我需要比较两个数字以确定n_1是否大于n_0 n_1 > n_0 显然,这并不是精确定义的,所以我定义n_1大于n_0,如果前面小于8个“数字”,否则小于n_0(如果不相等) 即如果: 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 in

我有两个循环整数,模16,所以它们假设值在0到15之间

我需要比较两个数字以确定
n_1
是否大于
n_0

n_1 > n_0
显然,这并不是精确定义的,所以我定义
n_1
大于
n_0
,如果前面小于8个“数字”,否则小于
n_0
(如果不相等)

即如果:

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
}
公共整数比较(整数n0,整数n1){
int ticksToZero=16-n0;
如果(n0==n1)
返回0;

否则如果((n1+ticksToZero)%16我从条件的简单部分开始,然后镜像它。
函数较小(n_0,n_1){
n=16;
n_0=n_0%n;
n_1=n_1%n;
如果(n_0==n_1)
返回0;
其他的
返回(n_0console.log(较小的(12,11));
您可以通过查找
n1
n0
的差异来测试它,并测试它是否介于1和8之间

#include <iostream>
using namespace std;

bool Test(int n0, int n1) {
    int n = (n1 - n0 + 16) % 16;
    return n && n <= 8;
}

int main() {
    cout << Test(0, 0) << endl;
    cout << Test(0, 1) << endl;
    cout << Test(0, 8) << endl;
    cout << Test(0, 9) << endl;
    cout << Test(0, 15) << endl;
    cout << endl;

    cout << Test(5, 0) << endl;
    cout << Test(5, 4) << endl;
    cout << Test(5, 5) << endl;
    cout << Test(5, 6) << endl;
    cout << Test(5, 13) << endl;
    cout << Test(5, 15) << endl;
    cout << endl;

    cout << Test(12, 0) << endl;
    cout << Test(12, 3) << endl;
    cout << Test(12, 4) << endl;
    cout << Test(12, 5) << endl;
    cout << Test(12, 12) << endl;
    cout << Test(12, 15) << endl;

    return 0;
}
#包括
使用名称空间std;
布尔测试(整数n0,整数n1){
int n=(n1-n0+16)%16;

返回n&&n使用此表达式,无需显式添加16即可执行此操作:

(b - a) >= (a <= b ? 8 : -8);

显然,这并不是精确定义的——事实上,它在数学上定义得很好,你在这里发明了一个轮子(一个与当前定义不匹配的轮子)@SomeWittyUsername:我完全确定我走的是我在数学上不理解的地面上。请启发我,我非常乐意学习如何用数学正确地表达这个问题?@SomeWittyUsername:研究一下,从这些问题来看,我似乎正确地指出它没有很好的定义:你是个骗子嗯,我站着更正了看起来像个问题。你在这里得到了它,它可以简化为以下表达式:
(n1+16-n0)%16
  0 1 2 3 4 5 6 7 8 9 A B C D E F
0                 * * * * * * * * 
1 *                 * * * * * * * 
2 * *                 * * * * * * 
3 * * *                 * * * * * 
4 * * * *                 * * * * 
5 * * * * *                 * * * 
6 * * * * * *                 * * 
7 * * * * * * *                 * 
8 * * * * * * * *                 
9   * * * * * * * *               
A     * * * * * * * *             
B       * * * * * * * *           
C         * * * * * * * *         
D           * * * * * * * *       
E             * * * * * * * *     
F               * * * * * * * *