【问题标题】:Counting the number of matching digits in an array at different positions计算数组中不同位置的匹配位数
【发布时间】:2019-03-09 15:55:43
【问题描述】:

定义:

  • Bulls:如果数组中匹配的数字在相同的位置
  • 奶牛:如果数组中匹配的数字在不同的位置

对于我的任务,我正在尝试编写计算公牛和奶牛数量的函数。

例如:

int[] secret = {2, 0, 6, 9};
int[] guessOne = {9, 5, 6, 2};
int[] guessTwo = {2, 0, 6, 2};
int[] guessThree = {1, 2, 3, 4, 5, 6};
int[] guessFour = {1, 3, 4, 4, 0, 5};

getNumOfBulls(secret, guessOne) returns 1.
getNumOfBulls(secret, guessTwo) returns 3.
getNumOfBulls(secret, guessThree) raises an exception.
getNumOfBulls(guessThree, guessFour) returns 2.
getNumOfCows(secret, guessOne) returns 2.
getNumOfCows(secret, guessTwo) returns 0.
getNumOfCows(secret, guessThree) raises an exception.
getNumOfCows(guessThree, guessFour) returns 2.

我能够完成第一部分,但我无法计算奶牛的数量。我的代码包括奶牛数量中的公牛数量,因此 getNumOfCows(secret, guessTwo) 返回 3 而不是 0。

这是我的代码:

// A method that gets the number of bulls in a guess

  public static int getNumOfBulls(int[] secretNumber, int[] guessedNumber) {

    // Initialize and declare a variable that acts as a counter

    int numberOfBulls = 0;

    if (guessedNumber.length == secretNumber.length) {

      // Compare the elements of both arrays at position i  

      for (int i = 0; i < guessedNumber.length; i++) {

        int guessedDigit = guessedNumber[i];
        int secretDigit = secretNumber[i];

        if (guessedDigit == secretDigit) {

          // Update the variable

          numberOfBulls++;
        }
      }
    }

    else {

      // Throw an IllegalArgumentException

      throw new IllegalArgumentException ("Both array must contain the same number of elements");
    }

    return numberOfBulls;
  }


  // A method that gets the number of cows in a guess --- TO BE FIXED

  public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {

    // Initialize and declare a variable that acts as a counter

    int numberOfCows = 0;

    if (guessedNumber.length == secretNumber.length) {

      // Loop through all the elements of both arrays to see if there is any matching digit located at different positions

      for (int i = 0; i < guessedNumber.length; i++) {

        for (int j = 0; j < secretNumber.length; j++) {

          int guessedDigit = guessedNumber[i];
          int secretDigit = secretNumber[j];

          if (guessedDigit == secretDigit) {

            // Update the varaible

            numberOfCows++;
          }
        }
      }
    }

    else {

      // Throw an IllegalArgumentException

      throw new IllegalArgumentException ("Both array must contain the same number of elements");
    }

    return numberOfCows;
  }

如何调整第二种方法以获得正确的奶牛数量?

【问题讨论】:

  • 你可以从你的数组中构造map,这样更容易实现所描述的问题解决方案
  • @Maya 您的问题需要的输出是什么?
  • 我认为您没有理解问题 guessTwo 应该返回 1(或 0,取决于规则)而不是 3 头奶牛。
  • @SyedMehtabHassan 输出显示在示例中
  • @JoakimDanielson 我知道guessTwo 应该返回0,但我的代码却返回3。我不知道如何解决这个问题

标签: java


【解决方案1】:

只需添加条件 i != j 即可解决您的问题,这样元素的位置就不会相同。 for (int i = 0; i

    for (int j = 0; j < secretNumber.length; j++) {

      int guessedDigit = guessedNumber[i];
      int secretDigit = secretNumber[j];

      if ( i != j && guessedDigit == secretDigit) {

        // Update the varaible

        numberOfCows++;
      }
    }

【讨论】:

  • @SyedMehtabHassan 您的代码消除了奶牛数量中的公牛数量。但是,getNumOfCows(secret,guessTwo) 仍将返回 1 而不是 0,因为guessTwo[3] 处的值与secret[0] 匹配。
  • @Maya 是不是应该返回 1,因为两个位置有 2 个?
  • 秘密数组索引 0 处的值 2 算作公牛,因此也不能视为牛
  • @Maya 您只需在 if 条件 guessedNumber[ i ] !=secretNumber[ i ] 中添加另一个检查,您的问题就会得到解决
【解决方案2】:

此解决方案将两个函数合并为一个,因为我们必须跟踪密钥中的哪些数字已被标记为公牛或牛。这意味着它将返回一个包含 2 个整数(公牛和奶牛)的数组,而不是一个整数。为清楚起见,我假设数组的大小相同。这个检查当然可以在方法被调用之前添加回来,甚至更好地执行。

public static int[] getNumOfBullsAndCows(int[] secretNumber, int[] guessedNumber) {
    int max = secretNumber.length;
    int cows = 0;
    int bulls = 0;
    int[] checked = new int[max];
    for (int i = 0; i < max; i++) {
        if (secretNumber[i] == guessedNumber[i]) {
          bulls++;
          checked[i] = 1;
        }
    }

    for (int i = 0; i < max; i++) {
      if (checked[i] == 1) {
        continue;
      }
      for (int j = 0; j < max; j++) {
        if (secretNumber[i] == guessedNumber[j]) {
          cows++;
          checked[i] = 1;
        }
      }
    }
    return new int[]{bulls, cows};
}

另一种选择,使用问题中的原始方法数牛,并使用我的解决方案仅数牛

public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {
    int max = secretNumber.length;
    int cows = 0;
    int[] checked = new int[max];
    for (int i = 0; i < max; i++) {
        if (secretNumber[i] == guessedNumber[i]) {
          checked[i] = 1;
        }
    }

    for (int i = 0; i < max; i++) {
      if (checked[i] == 1) {
        continue;
      }
      for (int j = 0; j < max; j++) {
        if (secretNumber[i] == guessedNumber[j]) {
          cows++;
          checked[i] = 1;
        }
      }
    }
    return cows;
}

【讨论】:

  • 难道不能保持方法的头部不变,而是返回 int 吗?
  • @你是什么意思,有两种方法而不是一种? (我现在发现我错过了将方法重命名为更有意义的名称)
  • 我不想返回一个数组,而是希望有 2 个返回 int 的方法
  • @Maya 当然,这更多的是实现细节,您可以保留 count Bulls 方法,仅将上述方法用于计数奶牛。我只是想避免两次做类似的事情。
  • 我的坏人刚刚看到它
猜你喜欢
  • 2018-10-17
  • 1970-01-01
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 2013-01-28
  • 1970-01-01
  • 2011-12-28
相关资源
最近更新 更多