0

我有一个包含五个值的整数数组,按升序排序,没有重复。我想检查数组中的五个值中的三个是否满足它们按顺序排列但并非全部都必须的要求。

如果以下一组数字中的三个在五个数字的数组中,则满足要求:

(1, 2, 3, 4)(5, 6, 7, 8)(9, 10, 11, 12) 等。

所以以下数组返回 true:

示例: (1, 2, 3, 18, 40)(2, 3, 4, 20, 34)(1, 3, 4, 7, 12)(9, 11, 12, 31, 51)

虽然以下数组返回 false:

示例: (3, 4, 5, 11, 29)(15, 16, 17, 33, 42) 等。

“规则”是五个数字的数组中的三个数字必须在各自的范围内,1、2、3、4或5、6、7、8等,但我不知道如何过去

有人可以帮我吗?

我将永远感激不尽!

编辑(因为作为新用户,我不能这么快就自己的问题发布答案):

好的,我找到了部分答案:首先我检查三张卡是否具有相同的等级。

我检查卡 1 到 3 是否具有相同的等级。如果不是,我检查卡片 2 到 4 是否具有相同的等级。如果不是,我检查卡片 3 到 5 是否具有相同的等级。这是我的代码:

// check
if (tempArray[0] == (tempArray[1] - 1)) {
    System.out.println("1. and 2. card match");
    if (tempArray[1] == (tempArray[2] - 1)) {
        System.out.println("2. and 3. card match");
        // three cards of same rank, starting from the first
        if (...) {
            isThreeOfAKind = true;
        }
    }
} else {
    System.out.println("1. and 2. card DO NOT match");
    // toak could start from the second card
    if (tempArray[1] == (tempArray[2] - 1)) {
        System.out.println("2. and 3. card match");
        if (tempArray[2] == (tempArray[3] - 1)) {
            System.out.println("3. and 4. card match");
            // three cards of same rank, starting from the second
            if (...) {
                isThreeOfAKind = true;
            }
        }
    } else {
        // toak could start from the third card
        System.out.println("2. and 3. card DO NOT match");
        if (tempArray[2] == (tempArray[3] - 1)) {
            System.out.println("3. and 4. card match");
            if (tempArray[3] == (tempArray[4] - 1)) {
                System.out.println("4. and 5. card match");
                // three cards of same rank, starting from the third
                if (...) {
                    isThreeOfAKind = true;
                }
            }
        }
    }
}

现在你注意到我省略了 if 子句。在那里我必须检查数字是否在范围内(1-4、5-8、9-12 等),但我不知道。!但是代码仍然有问题,因为例如 1、3、4 即使有效,也不会被识别为相同的等级

4

2 回答 2

0
public boolean check3InARow(int[] array) {
  int currentResult = (array[0] - 1) / 4;
  int countCurrentResult = 1;
  for (int i=1; i < array.length; i++) {
    if (((array[i] - 1) / 4) == currentResult) {
      countCurrentResult++
      if (countCurrentResult == 3) return true;
    } else {
      currentResult = (array[i] - 1) / 4;
      countCurrentResult = 1
    }
  }
  return false;
}
于 2013-01-20T00:25:37.027 回答
0

以下是我的解决方案 - 我认为这会有所帮助:

    public class CheckConsecutivity {

        public static boolean checkConsecutivity(int[] array) {
          final int NUMBER_OF_CONSECUTIVE_FOLLOWERS = 3; // this constant make the method general to check if any number have
                                                    // any number of consecutive followers; just change to the number you wish 4 or 10 e.g. 
          int count = 1;
          int currentPos = 0;
          for (int i = 1; i < array.length; i++) {
            if (array[currentPos] == array[i]-count) {
                System.out.println("Current Value = " + array[currentPos] + "; next value = "+array[i]  + "; count = " + count);
                count++;
                if ( count % (NUMBER_OF_CONSECUTIVE_FOLLOWERS + 1) == 0) {
                    System.out.println(NUMBER_OF_CONSECUTIVE_FOLLOWERS+" consecutive followers of "+array[currentPos]+" have been found :)");
                    return true;
                }
            } else {
                count = 1;
                i = currentPos++ + 1;
            }
          }
          System.out.println("consecutive numbers have not been found :(");
          return false;
        }

        public static void main(String[] args) {
          // TODO Auto-generated method stub
          int[] test = {7, 15, 10, 11, 12, 13, 1, 3, 4};
          int[] test1 = {4, 3, 2, 1};
          int[] test2 = {100, 101, 102, 17, 12, 1, 2, 5, 6, 7};

          System.out.println("\nTest evaluated to true++++++++++++++++++++++++++++++++:)"); 
          checkConsecutivity(test);

          System.out.println("\nTest1 evaluated to false------------------------------:("); 
          checkConsecutivity(test1);

          System.out.println("\nTest2 evaluated to false---------------------------------:(");  
          checkConsecutivity(test2);
        }

    }
于 2013-01-20T03:22:19.463 回答