繁体   English   中英

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

[英]Counting the number of matching digits in an array at different positions

定义:

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

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

例如:

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

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

只需添加一个条件 i != j 就可以解决您的问题,这样元素的位置就不会相同。 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 ( i != j && guessedDigit == secretDigit) {

        // Update the varaible

        numberOfCows++;
      }
    }

该解决方案将这两个函数合二为一,因为我们必须跟踪密钥中的哪些数字已经被标记为牛或牛。 这意味着它将返回一个包含 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM