下面的代码确实按照我需要的方式工作,但它很丑,过多或其他一些事情。我已经看了公式,并试图写一些解决方案,但我最终得到了类似数量的语句。

在这种情况下,是否有一种数学公式对我有益,或者是否可以接受16个if语句?

为了解释代码,这是一款基于同时回合制的游戏。两名玩家各有四个操作按钮,结果来自一个数组(0-3),但变量“1”和“2”可以赋值任何东西,如果这有帮助的话。结果是,0 =双方都不赢,1 = p1赢,2 = p2赢,3 =双方都赢。

public int fightMath(int one, int two) {

    if(one == 0 && two == 0) { result = 0; }
    else if(one == 0 && two == 1) { result = 0; }
    else if(one == 0 && two == 2) { result = 1; }
    else if(one == 0 && two == 3) { result = 2; }
    else if(one == 1 && two == 0) { result = 0; }
    else if(one == 1 && two == 1) { result = 0; }
    else if(one == 1 && two == 2) { result = 2; }
    else if(one == 1 && two == 3) { result = 1; }
    else if(one == 2 && two == 0) { result = 2; }
    else if(one == 2 && two == 1) { result = 1; }
    else if(one == 2 && two == 2) { result = 3; }
    else if(one == 2 && two == 3) { result = 3; }
    else if(one == 3 && two == 0) { result = 1; }
    else if(one == 3 && two == 1) { result = 2; }
    else if(one == 3 && two == 2) { result = 3; }
    else if(one == 3 && two == 3) { result = 3; }

    return result;
}

当前回答

由于您的数据集非常小,您可以将所有内容压缩为1个长整数并将其转换为公式

public int fightMath(int one,int two)
{
   return (int)(0xF9F66090L >> (2*(one*4 + two)))%4;
}

更多的位变体:

这利用了所有东西都是2的倍数这一事实

public int fightMath(int one,int two)
{
   return (0xF9F66090 >> ((one << 3) | (two << 1))) & 0x3;
}

神奇常数的起源

我能说什么呢?世界需要魔法,有时某些事情的可能性需要它的创造。

解决OP问题的函数的本质是从2个数字(1,2),域{0,1,2,3}到范围{0,1,2,3}的映射。每个答案都涉及了如何实现该映射。

此外,您可以在许多答案中看到问题的重述,即12个以4为基数的2位数字N(1,2)的映射,其中1是数字1,2是数字2,N = 4* 1 + 2;N ={0,1,2,…,15}——16个不同的值,这很重要。函数的输出是一个以4为基数的1位数字{0,1,2,3}——4个不同的值,这也很重要。

现在,1位以4为底的数可以表示为2位以2为底的数;{0,1,2,3} ={00,01,10,11},因此每个输出只能用2位编码。从上面来看,只有16种不同的输出可能,所以16*2 = 32位是编码整个地图所必需的;这些都可以装进一个整数。

常数M是映射M的编码,其中M(0)以位M[0:1]编码,M(1)以位M[2:3]编码,M (n)以位M[n*2:n*2+1]编码。

剩下的就是索引和返回常量的右边部分,在这种情况下,你可以将M右移2*N次,并取2个最低有效位,即(M >> 2*N) & 0x3。表达式(one << 3)和(two << 1)只是相乘,同时注意到2*x = x << 1和8*x = x << 3。

其他回答

感谢@Joe Harper,因为我最终使用了他的答案的变体。为了进一步瘦身,每4个结果中有2个是相同的,我进一步瘦身。

我可能会在某个时候回到这个问题上,但如果没有由多个if语句引起的主要阻力,那么我现在就保留这个问题。我将进一步研究表格矩阵和开关语句解决方案。

public int fightMath(int one, int two) {
  if (one === 0) {
    if (two === 2) { return 1; }
    else if(two === 3) { return 2; }
    else { return 0; }
  } else if (one === 1) {
    if (two === 2) { return 2; }
    else if (two === 3) { return 1; }
    else { return 0; }
  } else if (one === 2) {
    if (two === 0) { return 2; }
    else if (two === 1) { return 1; }
    else { return 3; }
  } else if (one === 3) {
    if (two === 0) { return 1; }
    else if (two === 1) { return 2; }
    else { return 3; }
  }
}

您可以创建包含结果的矩阵

int[][] results = {{0, 0, 1, 2}, {0, 0, 2, 1},{2, 1, 3, 3},{2, 1, 3, 3}};

当你想要获得价值时,你就会使用

public int fightMath(int one, int two) {
  return this.results[one][two]; 
}

我个人喜欢级联三元运算符:

int result = condition1
    ? result1
    : condition2
    ? result2
    : condition3
    ? result3
    : resultElse;

但在你的情况下,你可以使用:

final int[] result = new int[/*16*/] {
    0, 0, 1, 2,
    0, 0, 2, 1,
    2, 1, 3, 3,
    1, 2, 3, 3
};

public int fightMath(int one, int two) {
    return result[one*4 + two];
}

或者,你可以注意到比特的模式:

one   two   result

section 1: higher bits are equals =>
both result bits are equals to that higher bits

00    00    00
00    01    00
01    00    00
01    01    00
10    10    11
10    11    11
11    10    11
11    11    11

section 2: higher bits are different =>
lower result bit is inverse of lower bit of 'two'
higher result bit is lower bit of 'two'

00    10    01
00    11    10
01    10    10
01    11    01
10    00    10
10    01    01
11    00    01
11    01    10

所以你可以使用魔法:

int fightMath(int one, int two) {
    int b1 = one & 2, b2 = two & 2;
    if (b1 == b2)
        return b1 | (b1 >> 1);

    b1 = two & 1;

    return (b1 << 1) | (~b1);
}

当我在1 / 2和结果之间画一个表时,我看到了一个模式,

if(one<2 && two <2) result=0; return;

以上将减少至少3个if语句。我没有看到一个集合模式,也无法从给出的代码中收集到很多信息——但如果可以推导出这样的逻辑,它将减少大量的if语句。

希望这能有所帮助。

静态int val(int i, int u){ Int q = (i & 1) ^ (u & 1); 返回(i >> 1) << (1 ^ q))|((u >> 1) << q); }