计算位于不同位置的整数数组中公共元素的数量 [英] Counting the number of common elements in integer arrays located at different positions

查看:105
本文介绍了计算位于不同位置的整数数组中公共元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的作业,我需要编写一个方法,该方法返回在2个数组之间找到的母牛的数量(请参阅下面的定义)。如果输入数组具有不同数量的元素,则该方法应引发IllegalArgumentException并附带一条适当的消息。

For my assignment, I need to write a method that returns the number of cows (see definition below) found between 2 arrays. If the input arrays have a different number of elements, then the method should throw an IllegalArgumentException with an appropriate message.

公牛是int数组中位于int数组中的公用数字。相同的位置,而母牛是在不同位置的int数组中的公用数。请注意,如果数字已经是公牛,则不能将其视为母牛。

A bull is a common number in int arrays found at the same position while a cow is a common number in int arrays found at different position. Note that if a number is already a bull, it cannot be considered as a cow.

例如,考虑以下数组:

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

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

我在下面看到的方法对于示例1和3,但是示例2和示例4存在问题,例如getNumOfCows(secret,guessTwo)返回1而不是0,因为secret [0]和guessTwo [3]上的元素被视为母牛。有人可以帮我修复我的代码吗?

My method seen below works perfectly for examples 1 and 3, but there is a problem with examples 2 and 4 such that getNumOfCows(secret, guessTwo) returns 1 instead of 0 because the element at secret[0] and guessTwo[3] is considered a cow. Could anybody help me fix my code?

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

    // Initialize and declare an array

    int[] verified = new int[secretNumber.length];

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

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

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

        // Check if the digits represent a bull

        if (guessedNumber[i] == secretNumber[i]) {

          verified[i] = 1;
        }
      }

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

        // Continue to the next iteration if the digits represent a bull

        if (verified[i] == 1) {

          continue;
        }

        else {

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

            if (guessedNumber[i] == secretNumber[j] && i != j) {

              // Update the variable

              numberOfCows++;

              verified[i] = 1;

            }
          }
        }
      }
    }

    else {

      // Throw an IllegalArgumentException

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

    return numberOfCows;
  }


推荐答案

首先检查并标记所有公牛使用单独的数组以确保属于公牛的头寸也被计为牛

First go through and mark all bulls using a separate array to make sure a position that is a bull also get counted as a cow

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






现在原始问题可以被投票为重复的内容。


Now that this answer is accepted the original question can be voted to be closed as a duplicate

我在此处发布重复问题的答案,如果得到批准,则另一个可以作为副本关闭。

I am posting my answer from a duplicate question here and if this get approved then the other one can get closed as a duplicate.

这篇关于计算位于不同位置的整数数组中公共元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆