|
C语言趣味程序百例精解之java实现(55)谁值班
分析:
程序:
public class Test55{
public static void main(String args[]){
new Test55().whoOnDuty55();
}
/**
* 55。谁值班
*/
public void whoOnDuty55() {
for (int a = 1; a <= 7; a++)
for (int b = 1; b <= 7; b++)
for (int c = 1; c <= 7; c++)
for (int d = 1; d <= 7; d++)
for (int e = 1; e <= 7; e++)
for (int f = 1; f <= 7; f++)
for (int g = 1; g <= 7; g++) {
if ((a - c == 1)
&& (d - e == 2)
&& (g - b == 3)
&& (f == 4)
&& ((b > 4 && c < 4) || (b < 4 && c > 5))
&& (a + b + c + d + e + f + g == (1 + 7) / 2 * 7)
&& notEquls(new int[] { a, b, c, d,
e, f, g }) == true) {
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
System.out.println("d=" + d);
System.out.println("e=" + e);
System.out.println("f=" + f);
System.out.println("g=" + g + "\n\n");
}
}
}
/**
* 判断是否两两不相等
*/
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>java Test55
a=7
b=2
c=6
d=3
e=1
f=4
g=5
E大夫是星期一,B大夫是星期二,D大夫是星期三,F大夫是星期四,G大夫是星期五,C大夫是
星期六,A大夫是星期日。 |
|