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は有効であるにもかかわらず同じランクとして認識されないため、コードにはまだ誤りがあります。