数学趣题——谁在说谎
转载链接:http://www.cnblogs.com/steven_oyj/archive/2010/05/26/1744227.html
问题:A说:B说谎。B说:C说谎。C说:A.B都说谎。请问谁说谎
分析:
对A,B,C三人说的话的真假变量a,b,c表示,a,b,c的取值为1或0
按照题意:存在如下逻辑关系:
(1)若a = 1, 则 b = 0;
(2)若a = 0, 则 b = 1;
(3)若b = 1, 则 c = 0;
(4)若b = 0, 则 c = 1;
(5)若c = 1, 则 a+b = 0;
(6)若c = 0, 则 a+b != 0;
总结关系式为:
(a==1)&&(b==0) || (a==0)&&(b==1) || (b==1)&&(c==0) || (b==0)&&(c==1) || (c==1)&&(a+b==0) || (c==0)&&(a+b != 0)
化简后为(a && !b || !a && b) && (b && !c || !b && c) && (c && (a + b == 0) || !c && (a + b != 0) )
所以凡是不满足以上条件的答案组合就不是正确答案,可用穷举法检测所有解空间
穷举法源码:
1: #include <stdio.h>
2:
3: int main()
4: {
5: int a, b, c;
6:
7: for(a = 0; a <= 1; a++)
8: for(b = 0; b <= 1; b++)
9: for(c = 0; c <= 1; c++)
10: if((a && !b || !a && b) && (b && !c || !b && c) && (c && a + b == 0 || !c && a + b != 0))
11: {
12: printf("甲 told a %s
", a ? "truth" : "lie");
13: printf("乙 told a %s
", b ? "truth" : "lie");
14: printf("丙 told a %s
", c ? "truth" : "lie");
15: }
16:
17: return 0;
18: }