首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反之亦然,对值及其变量进行编码

反之亦然,对值及其变量进行编码
EN

Stack Overflow用户
提问于 2021-10-09 08:29:00
回答 2查看 68关注 0票数 1

我有两个变量A和B,如果A =1,那么B应该B=2,如果是A=2,那么B应该像这样,有3对B=1,这是编写代码的最好方法是什么,而不仅仅是if - 1-2,3-4,5-6

EN

回答 2

Stack Overflow用户

发布于 2021-10-09 09:16:30

可以使用简单的加法和减法来获得两个(x, x + 1)的另一个元素

代码语言:javascript
复制
int a = 1; // the other is 2, sum is 3
int b = 3 - a;  // if a = 2, b = 1

int c = 3; // the other is 4, sum is 7
int d = 7 - c; // if c = 4, d = 3

int m = 5; // the other is 6, sum is 11
int n = 11 - m;

另一种方法可以使用以下逻辑:

代码语言:javascript
复制
if (a % 2 == 1) b = a + 1;
else b = a - 1;

因此,可以使用数组来提供+/- 1:

代码语言:javascript
复制
static int[] signs = {-1, 1};
public static int nextWithArrPositive(int a) {
    return a + signs [a % 2];
}

此表达式无法用于负a,因为在本例中,需要a % 2 == -1和更高级的逻辑来正确计算值,以考虑负余数:

代码语言:javascript
复制
public static int nextWithArr(int a) {
    int sign = (a & 0x80000000) >> 31; //-1 if a < 0, 0 otherwise
    // a >= 0 : 0 - even, 1 - odd;
    // a  < 0 : 1 - even, 0 - odd
    return a + signs[a % 2 - sign];
}

但是,可以设计一个更简单的表达式:

代码语言:javascript
复制
public static int nextWithMod(int a) {
    return a + a % 2 - (a - 1) % 2;
}

让我们比较一下三种实现的结果,包括user3386109评论中提供的xor解决方案b = ((a - 1) ^ 1) + 1

代码语言:javascript
复制
public static int nextXor(int a) {
    return ((a - 1) ^ 1) + 1;
}

测试:

代码语言:javascript
复制
System.out.println("+-----+-----+-----+-----+");
System.out.println("|  a  | arr | mod | xor |");
System.out.println("+-----+-----+-----+-----+");
for (int i = -6; i < 7; i++) {
    System.out.printf("| %2d  | %2d  | %2d  | %2d  |%n", i, nextWithArr(i), nextWithMod(i), nextXor(i));
}
System.out.println("+-----+-----+-----+-----+");

输出:

代码语言:javascript
复制
+-----+-----+-----+-----+
|  a  | arr | mod | xor |
+-----+-----+-----+-----+
| -6  | -5  | -5  | -7  |
| -5  | -6  | -6  | -4  |
| -4  | -3  | -3  | -5  |
| -3  | -4  | -4  | -2  |
| -2  | -1  | -1  | -3  |
| -1  | -2  | -2  |  0  |
|  0  | -1  |  1  | -1  |
|  1  |  2  |  2  |  2  |
|  2  |  1  |  1  |  1  |
|  3  |  4  |  4  |  4  |
|  4  |  3  |  3  |  3  |
|  5  |  6  |  6  |  6  |
|  6  |  5  |  5  |  5  |
+-----+-----+-----+-----+
票数 2
EN

Stack Overflow用户

发布于 2021-10-09 09:21:47

一个简单的解决方案是表查找。在一个数组中,对于a的每个可能值,我存储b的相应值

代码语言:javascript
复制
private static final int[] B_PER_A = { -1, 2, 1, 4, 3, 6, 5 };

因为在Java中数组索引总是从0开始,所以我们需要在索引0处放置一个虚拟值。这个值永远不会使用(或者至少永远不应该使用)。

让我们试试看:

代码语言:javascript
复制
    for (int a = 1; a <= 6; a++) {
        int b = B_PER_A[a];
        System.out.format("a: %d; b: %d.%n", a, b);
    }

输出:

a: 1;b: 2. a: 2;b: 1. a: 3;b: 4. a: 4;b: 3. a: 5;b: 6. a: 6;b: 5

推广到3对以上

如果您需要处理数量可变的对,请求助于数学。

代码语言:javascript
复制
public static int calcB(int a) {
    // 0-based index of pair (0 = 1-2, 1 = 3-4, etc.)
    int pairNumber = (a - 1) / 2;
    // a + b for given pair
    int pairSum = 4 * pairNumber + 3;
    int b = pairSum - a;
    return b;
} 

在每一对中,和等于3模4。我正在利用这一事实来求出给定对的和。当我从这个和中减去a时,我得到了b。让我们也来看看这个演示:

代码语言:javascript
复制
    for (int a = 1; a <= 8; a++) {
        int b = calcB(a);
        System.out.format("a: %d; b: %d.%n", a, b);
    }

a: 1;b: 2. a: 2;b: 1. a: 3;b: 4;b: 3. a: 5;b: 6. a: 6;b: 5. a: 7;b: 8. a: 8;b: 7.

后一种解决方案更复杂,也更难阅读。因此,如果您总是有三个对,不多也不少,我建议先给出更简单的表查找。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69505024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档