0

5つの値を持つ整数配列があり、重複なしで昇順で並べ替えられています。配列にある5つの値のうち3つが、それらが順番に並んでいるという要件を満たしているかどうかを確認したいのですが、すべてがそうである必要はありません。

以下の数字のグループのうち3つが5つの数字の配列に含まれている場合、要件は満たされています。

(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) など。

「ルール」は、5つの数字の配列内の3つの数字が、それぞれの範囲内にある必要があるということです。1、2、3、4または5、6、7、8などですが、これを調べる方法がわかりません。

誰かがこれを手伝ってくれませんか?

私は永遠に感謝します!

編集(新しいユーザーとして、私は自分の質問への回答をすぐに投稿できないため):

さて、私は部分的な答えを見つけました:最初に私は3枚のカードが同じランクを持っているかどうかをチェックします。

カード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 に答える