计算数组中不同位置的匹配位数

时间:2019-03-09 15:55:44

标签: java

定义:

  • 公牛:如果数组中的匹配数字位于相同位置
  • 母牛:如果数组中的匹配数字在不同位置

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

例如:

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

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

2 个答案:

答案 0 :(得分:0)

只需添加条件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++;
      }
    }

答案 1 :(得分:0)

此解决方案将这两个功能合并为一个功能,因为我们必须跟踪秘密中已被标记为牛或牛的数字。这意味着它将返回一个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;
}