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

时间:2017-12-15 12:29:57

标签: algorithm math cycle modulus

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

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

n_1 > n_0

显然,这并没有完全定义,所以如果n_1小于8,那么我将n_0定义为大于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. (如果不相等)。

即。如果:

requests.post('https://accounts.google.com/o/oauth2/revoke',
    params={'token': credentials.token},
    headers = {'content-type': 'application/x-www-form-urlencoded'})

如何以编程方式表达此比较?

我确信我对上面的术语感到困惑,所以请随意纠正我的措辞。 :)

4 个答案:

答案 0 :(得分:1)

我正在考虑一个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
}

答案 1 :(得分:1)

您可以通过查找n1n0的差异对其进行测试,并测试它是否在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;
}

答案 2 :(得分:1)

你可以使用这个表达式在没有明确添加16的情况下完成它:

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

根据比较ab的结果,差异必须高于8或-8。

将此公式应用于数字0..15(包括0和15)的结果如下(星号表示水平线中的数字小于垂直线上的数字的点;十六进制数字用于表示上面的数字9; demo

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

答案 3 :(得分:0)

我从条件的简单部分开始,然后镜像它。

    function smaller(n_0, n_1) {
      n = 16;   
      n_0 = n_0 % n;
      n_1 = n_1 % n;
    
      if(n_0 == n_1)
          return 0;
      else
          return (n_0 < n_1 && n_1 <= n_0 + 8) || (n_1 < n_0 &&  n_0 >= n_1 + 8);
    }
    console.log(0);
    console.log(smaller(0,1));
    console.log(smaller(0,8));
    console.log(smaller(0,9));
    console.log(5);
    console.log(smaller(5,6));
    console.log(smaller(5,15));
    console.log(smaller(5,16));
    console.log(12);
    console.log(smaller(12,13));
    console.log(smaller(12,14));
    console.log(smaller(12,15))
    console.log(smaller(12,0));
    console.log(smaller(12,1))
    console.log(smaller(12,2))
    console.log(smaller(12,3))
    console.log(smaller(12,4));
    console.log(smaller(12,5));
    console.log(smaller(12,6));
    console.log(smaller(12,7));
    console.log(smaller(12,8));
    console.log(smaller(12,9));
    console.log(smaller(12,10));
    console.log(smaller(12,11));

相关问题