|
C语言趣味程序百例精解之java实现: 62.奇特立方体
程序:
public class Test62{
public static void main(String args[]){
int x[]=new int[8];
for(int i=0;i<8;i++){
x=Integer.parseInt(args);
// System.out.println(x);
}
new Test62().cube62(x);
}
/**
* 62.cube立方体
*/
public void cube62(int x[]) {
int n = 8;
boolean success=false;
for (int a = 0; a < n; a++)
for (int b = 0; b < n; b++)
for (int c = 0; c < n; c++)
for (int d =0; d < n; d++)
for (int e = 0; e < n; e++)
for (int f = 0; f < n; f++)
for (int g = 0; g < n; g++)
for (int h =0; h < n; h++) {
if (notEquls(new int[] { x[a],x, x[c],x[d],x[e],x[f],x[g],x[h]})) {
if(cube62(x[a],x, x[c], x[d], x[e], x[f], x[g], x[h])) return;
}
}
if(success!=true) System.out.println("不能构成奇妙立方体");;
}
public boolean cube62(int a, int b, int c, int d, int e, int f, int g, int h) {
boolean success=false;
int s = a + b + c + d;
if (s == e + f + g + h)
if (s == a + b + e + f)
if (s == c + d + g + h)
if (s == a + c + e + g)
if (s == b + d + f + h) {
System.out.println("能构成奇妙立方体");
System.out.print(" a=" + a);
System.out.print(" b=" + b);
System.out.print(" c=" + c);
System.out.print(" d=" + d);
System.out.print(" e=" + e);
System.out.print(" f=" + f);
System.out.print(" g=" + g);
System.out.print(" h=" + h);
return true;
}
return success;
}
/**
* 判断是否两两不相等
*/
public boolean notEquls(int[] a) {
if (a == null || a.length == 0 || a.length == 1)
return true;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a == a[j] && i != j) {
// System.out.println("a[" + i + "]" + a + " a[" + j +
// "]"
// + a[j] + "---");
return false;
}
}
}
return true;
}
}
C:\bat>javac Test62.java
C:\bat>java Test62 20 21 22 23 24 25 26 27
能构成奇妙立方体
a=20 b=23 c=25 d=26 e=27 f=24 g=22 h=21
C:\bat>java Test62 1 2 3 4 5 6 7 9
不能构成奇妙立方体
C:\bat>java Test62 1 2 3 4 5 6 7 8
能构成奇妙立方体
a=1 b=4 c=6 d=7 e=8 f=5 g=3 h=2 |
|