小明最近喜欢搭数字积木,
一共有10块积木,每个积木上有一个数字,0~9。
搭积木规则:
每个积木放到其它两个积木的上面,并且一定比下面的两个积木数字小。
最后搭成4层的金字塔形,必须用完所有的积木。
package the_blue_cup;
//搭积木
import java.util.Arrays;
public class Demo_10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long[][] triangle = new long[4][];
long[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
long[] temp = new long[numbers.length];
triangle[0] = new long[1];
triangle[1] = new long[2];
triangle[2] = new long[3];
triangle[3] = new long[4];
System.out.println(generateAndCheckList(numbers, temp));
// //从临时存储中取出数组给三角形
// for (int i = 0; i < store.length; i++) {
// for (int j = 0, l = 0; j < triangle.length && l < 10; j++) {
// for (int k = 0; k < triangle[j].length; k++) {
// triangle[j][k] = store[i][l];
//
// if (triangle[j][k] != 0 && l <= 8) {
// l++;
// }
// }
// }
//
//
//
//
// }
}
//该方法负责产生和检查符合要求的数组
public static int generateAndCheckList(long[] numbers, long[] temp) {
//只需要检查第1到6个数字即可,即1~3行
boolean level1, level2, level3, level4, level5, level6;
//默认第1~6个数字检查结果都为假
level1 = level2 = level3 = level4 = level5 = level6 = false;
//负责计数
int count = 0;
//这里用一个十层的for循环,并用continue结束不符合要求的迭代,从而大大压缩执行时间
//temp[0]~temp[9]临时存放数字
//layer1~layer10表示第1~10个数字
for (int layer1 = 0; layer1 < temp.length; layer1++) {
temp[0] = layer1;
for (int layer2 = 0; layer2 < temp.length; layer2++) {
if (layer2 == layer1) {
continue;
}
temp[1] = layer2;
for (int layer3 = 0; layer3 < temp.length; layer3++) {
if (layer3 == layer2 || layer3 == layer1) {
continue;
}
temp[2] = layer3;
if (layer1 < layer2 && layer1 < layer3 ) {
level1 = true;
}else {
continue;
}
for (int layer4 = 0; layer4 < temp.length; layer4++) {
if (layer4 == layer2 || layer4 == layer1 || layer4 == layer3) {
continue;
}
temp[3] = layer4;
for (int layer5 = 0; layer5 < temp.length; layer5++) {
if (layer5 == layer2 || layer5 == layer1 || layer5 == layer3 || layer5 == layer4) {
continue;
}
temp[4] = layer5;
if (layer2 < layer4 && layer2 < layer5) {
level2 = true;
}else {
continue;
}
for (int layer6 = 0; layer6 < temp.length; layer6++) {
if (layer6 == layer2 || layer6 == layer1 || layer6 == layer3 || layer6 == layer4 || layer6 == layer5) {
continue;
}
temp[5] = layer6;
if (layer3 < layer5 && layer3 < layer6) {
level3 = true;
}else {
continue;
}
for (int layer7 = 0; layer7 < temp.length; layer7++) {
if (layer7 == layer2 || layer7 == layer1 || layer7 == layer3 || layer7 == layer4 || layer7 == layer5 || layer7 == layer6) {
continue;
}
temp[6] = layer7;
for (int layer8 = 0; layer8 < temp.length; layer8++) {
if (layer8 == layer2 || layer8 == layer1 || layer8 == layer3 || layer8 == layer4 || layer8 == layer5 || layer8 == layer6 || layer8 == layer7) {
continue;
}
temp[7] = layer8;
if (layer4 < layer7 && layer4 < layer8) {
level4 = true;
}else {
continue;
}
for (int layer9 = 0; layer9 < temp.length; layer9++) {
if (layer9 == layer2 || layer9 == layer1 || layer9 == layer3 || layer9 == layer4 || layer9 == layer5 || layer9 == layer6 || layer9 == layer7 || layer9 == layer8) {
continue;
}
temp[8] = layer9;
if (layer5 < layer8 && layer5 < layer9) {
level5 = true;
}else {
continue;
}
for (int layer10 = 0; layer10 < temp.length; layer10++) {
if (layer10 == layer2 || layer10 == layer1 || layer10 == layer3 || layer10 == layer4 || layer10 == layer5 || layer10 == layer6 || layer10 == layer7 || layer10 == layer8 || layer10 == layer9) {
continue;
}
temp[9] = layer10;
if (layer6 < layer9 && layer6 < layer10) {
level6 = true;
}else {
continue;
}
if (level1 && level2 && level3 && level4 && level5 && level6) {
count++;
}
System.out.println(Arrays.toString(temp));
}
}
}
}
}
}
}
}
}
}
return count;
}
}
运行结果:768