我有一个包含五个值的整数数组,按升序排序,没有重复。我想检查数组中五个值中的三个是否满足它们在序列中的要求,但并非所有值都必须。
如果下面的一组数字中有三个是五个数字的数组,则满足要求:
(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不被认为是相同的等级,即使它是有效的
答案 0 :(得分: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;
}
答案 1 :(得分: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);
}
}