|
C语言趣味程序百例精解之java实现: 59.填表格
代码:
public class Test59{
public static void main(String args[]){
new Test59().TianTable59();
}
/**
* 59.填表格
*
* a b c
*
* d e f
*/
public void TianTable59() {
for (int a = 1; a <= 6; a++)
for (int b = 1; b <= 6; b++)
for (int c = 1; c <= 6; c++)
for (int d = 1; d <= 6; d++)
for (int e = 1; e <= 6; e++)
for (int f = 1; f <= 6; f++) {
if (b > a
&& c > b
&& e > d
&& f > e
&& d > a
&& e > b
&& f > c
&& notEquls(new int[] { a, b, c, d, e,
f })) {
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.println(" f=" + f);
}
}
}
/**
* 判断是否两两不相等
*/
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 = i; 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>java Test59
a=1 b=2 c=3 d=4 e=5 f=6
a=1 b=2 c=4 d=3 e=5 f=6
a=1 b=2 c=5 d=3 e=4 f=6
a=1 b=3 c=4 d=2 e=5 f=6
a=1 b=3 c=5 d=2 e=4 f=6 |
|