【问题标题】:Check if cyclic (modulo 16) number is larger than another?检查循环(模 16)数是否大于另一个?
【发布时间】:2017-12-15 12:29:56
【问题描述】:

我有两个循环整数,以 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.

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

我确信我混淆了上面的术语,所以请随时更正我的措辞。 :)

【问题讨论】:

  • 显然,这并没有完全定义 - 实际上,它在数学上定义得非常好,你在这里发明了一个轮子(并且一个与当前定义不匹配的轮子) .
  • @SomeWittyUsername:我完全确定我走在我不理解数学的地面上。请赐教,我会非常乐意学习如何正确地数学表达问题?
  • @SomeWittyUsername:研究了一下,从这个问题看来,我说它没有明确定义是对的:math.stackexchange.com/questions/1211454/…
  • 你是对的,我的立场是正确的
  • 看起来像XY problem 问题。

标签: algorithm math cycle modulus


【解决方案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
}

【讨论】:

  • 你知道了,它可以简化为表达式:(n1 + 16 - n0) % 16 &lt;= 8。非常感谢:)
【解决方案2】:

你可以通过找出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;
}

【讨论】:

    【解决方案3】:

    您可以使用以下表达式在不显式添加 16 的情况下做到这一点:

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

    这个想法是,根据ab 的比较结果,差异必须在 8 或 -8 以上。

    将此公式应用于数字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               * * * * * * * *   
    

    【讨论】:

      【解决方案4】:

      我从条件的简单部分开始,然后对其进行镜像。

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

      【讨论】:

        猜你喜欢
        • 2011-04-19
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多