java 查找范围内的较大数字

bogh5gae  于 2023-01-07  发布在  Java
关注(0)|答案(6)|浏览(159)

我一直在这些CodingBat问题,我遇到了另一个问题。
任务是这样的:
给定2个正int值,返回10到20范围内的较大值,如果两者都不在该范围内,则返回0。
一些例子:

  • 最大值1020(11,19)→ 19
  • 最大值1020(19,11)→ 19
  • 最大值1020(11,9)→ 11

我的解决方案是:

public int max1020(int a, int b) {
    if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && a >= b) {
        return a;
    }

    if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && b >= a) {
        return b;
    } else {
        return 0;
    }
}

代码在超过一半的时间内返回了正确的输出,但在某些情况下并没有按预期工作。这些情况是当输入为(10,21)、(21,10)、(23,10)时。
这很奇怪,因为我显式地排除了#〉20,但它在上述三个场景中分别返回21、21和23。
我哪里做错了?

pu3pd22g

pu3pd22g1#

让我们走过他们。

(10,21):

if(((10 >= 10 && 10 <= 20) || (21>=10 && 21<=20)) && 10>=21)
  (((true and true) || (true and false)) && false)
  ((true) && false)
  false

好吧,不是那个。

if(((10 >= 10 && 10 <= 20) || (21>=10 && 21<=20)) && 21>=10)
  (((true and true) || (true and false)) && false)
  ((true) and true)
  true

-> return 21

好吧,为什么会这样呢?因为你的逻辑说“如果任一个值在范围内,那么返回较大的值,即使较大的值不在范围内”。
相反,如果某个值超出了范围,就不要考虑它。

if(a < 10 && a > 20) {
    // do something with only b. This is where you would return zero, too.
} else if(b < 10 && b > 20) {
    // do something with only a
} else {
    // do something with both
}
t1rydlwq

t1rydlwq2#

你的逻辑基本上是:

  • 如果ab在范围内,并且a大于或等于b,则返回a
  • 如果ab在范围内,并且b大于或等于a,则返回b
  • 返回0。

因此,如果a在范围内,但b较大(并且超出范围),则仍然返回b
将您的逻辑更改为:

  • 如果a在范围内,并且a大于或等于b,则返回a
  • 如果b在范围内,并且b大于或等于a,则返回b
  • 返回0。
f4t66c6m

f4t66c6m3#

Boolean aInRange = (a >= 10 && a <= 20)

Boolean bInRange = (b >= 10 && b <= 20)

int max = 0;

if (aInRange && bInRange) {

    if(a >= b)
        max = a;
    else {
        max = b;
    }
} else if (!bInRange && aInRange) {
    max = a;
}
else if (bInRange && !aInRange) {
    max = b;
}
return max;
pbgvytdp

pbgvytdp4#

如果其中一个值在范围内,代码将返回较大的值。
考虑到这些问题的性质,您应该尝试使用test driven approach来开发您的方法,这也可以确保您的代码按照您的预期运行。当您在CodingBat上提交代码时,可能会使用与下面类似的测试来测试您的代码。

public class SandBox {
    public int max1020(int a, int b) {
        if (10 <= a && a <= 20) { // if a is in range
            if (a >= b || b > 20) { // if a is greater than B or B is out of range
                return a;
            }
        }

        //
        if (10 <= b && b <= 20) { // if b is in range
            return b;
        }

        return 0;
    }
}

测试

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class SandBoxTest {
    SandBox sand;

    @Before
    public void given(){
        sand = new SandBox();
    }

    @Test
    public void testOne(){
        int i = sand.max1020(1, 2);
        assertThat(i, is(0));
    }

    @Test
    public void testTwo(){
        int i = sand.max1020(2, 1);
        assertThat(i, is(0));
    }

    @Test
    public void testThree(){
        int i = sand.max1020(5, 10);
        assertThat(i, is(10));
    }

    @Test
    public void testFour(){
        int i = sand.max1020(10, 5);
        assertThat(i, is(10));
    }

    @Test
    public void testFive(){
        int i = sand.max1020(11, 15);
        assertThat(i, is(15));
    }

    @Test
    public void testSix(){
        int i = sand.max1020(15, 11);
        assertThat(i, is(15));
    }

    @Test
    public void testSeven(){
        int i = sand.max1020(20, 23);
        assertThat(i, is(20));
    }

    @Test
    public void testEight(){
        int i = sand.max1020(23, 20);
        assertThat(i, is(20));
    }

    @Test
    public void testNine(){
        int i = sand.max1020(33, 25);
        assertThat(i, is(0));
    }

    @Test
    public void testTen(){
        int i = sand.max1020(25, 33);
        assertThat(i, is(0));
    }
}
xytpbqjk

xytpbqjk5#

我想知道其他人是如何解决的,这是我想到的,其中包括三元和一点Math.abs + Math.max方法:

public int max1020(int a, int b) {
  if(Math.abs(a - 15) <= 5 & Math.abs(b - 15) <= 5)
    return Math.max(a, b);
  if(Math.abs(a - 15) > 5)
    return Math.abs(b - 15) <= 5 ? b : 0;
  
  return a;
}
xmq68pz9

xmq68pz96#

下面是我提出的一个算法,它可以找到数组中的最大值,但附加条件是在特定范围内。我认为它更灵活,更容易阅读)

public int max1020(int a, int b) {
    int max = 0;
    int[] numbers= {a,b};
    for (int i  = 0; i < numbers.length; i++) {
      if (numbers[i] > max &&  numbers[i] >=10 && numbers[i] <=20 ) {
        max = numbers[i];
      }
    }
    return max;

}

相关问题