|
C语言趣味程序百例精解之java实现(53)博士的难题1
分析:
假设这三个人分别为a,b,c,若说谎其值为0,若诚实其值为1,根据题目中三个人的话可分别列出:
程序解答:
public class Test53{
public static void main(String args[]){
whoTellLie53();
}
/**
* 53. 谁在说谎
*/
public static void whoTellLie53() {
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
if (((a == 1 && a + b + c == 2) || (a == 0 && a + b + c != 2))
&& ((b == 1 && a + b + c == 1) || (b == 0 && a + b + c != 1))
&& ((c == 1 && a + b + c == 1) || (c == 0 && a + b + c != 1))) {
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}
}
C:\bat>java Test53
a=0
b=0
c=0三人都来自说谎族。 |
|