我正在尝试使用Java来解决以下问题。
鉴于多米诺骨牌的安排,确定它是否是一种法律安排,并相应地返回真或假。 例如,这是一种非法安排 [2 3] [4 2] [2 2] [3 5] 这是多米诺的法律安排。 [2 3] [3 5] [5 4] [4 5]。
我不知道如何开始写这个程序。
答案 0 :(得分:0)
编写Domino类
public class Domino {
private int leftValue;
private int rightValue;
public Domino(int leftValue, int rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public int getLeftValue() {
return leftValue;
}
public int getRightValue() {
return rightValue;
}
}
创建一个包含多米诺骨牌
的数组或列表Domino[] dominos = new Domino[] {
new Domino(2, 3),
new Domino(3, 2),
new Domino(2, 5),
new Domino(2, 5)
};
循环并匹配左右值
Domino previous = null;
for (Domino current : dominos) {
if (previous != null) {
if (current.getLeftValue() != previous.getRightValue()) {
throw new Exception("Illegal arrangement detected");
}
}
previous = current;
}