黑与白(逻辑推理)

黑与白:有 A、B、C、D、E 这 5 个人,每个人额头上都贴了一张黑或白的纸。5 人对坐,每个人都可以看到其他人额头上纸的颜色。5 人相互观察后,
A 说:“我看见有 3 人额头上贴的是白纸,1 人额头上贴的是黑纸。”
B 说:“我看见其他 4 人额头上贴的都是黑纸”
C 说:“我看见 1 人额头上贴的是白纸,其他 3 人额头上贴的是黑纸”
D 说:“我看见 4 人额头上贴的都是白纸”
E 什么也没说。
现在已知额头上贴黑纸的人说的都是谎话,额头贴白纸的人说的都是实话。问这 5 人谁的额头上贴的是白纸,谁的额头上贴的是黑纸?

#0为黑 假 1为白 真
for a in range(2):
    for b in range(2):
        for c in range(2):
            for d in range(2):
                for e in range(2):
                    #每个都有两种可能非真及假
                    if (((a and b+c+d+e==3) or (not a and b+c+d+e!=3)) and \
                        ((b and a+c+d+e==0) or (not b and a+c+d+e!=0)) and\
                        ((c and a+b+d+e==1) or (not c and a+b+d+e!=1)) and\
                        ((d and a+b+c+e==4) or (not d and a+b+c+e!=4))):
                    #这样写也行
                    # if (((a == 1 and b + c + d + e == 3) or (a == 0 and b + c + d + e != 3)) and \
                    #         ((b == 1 and a + c + d + e == 0) or (b == 0 and a + c + d + e != 0)) and \
                    #         ((c == 1 and a + b + d + e == 1) or (c == 0 and a + b + d + e != 1)) and \
                    #         ((d == 1 and a + b + c + e == 4) or (d == 0 and a + b + c + e != 4))):
                        k=[a,b,c,d,e]
                        v=["a","b","c","d","e"]

                        for i in range(len(k)):
                            print("{}白纸 ".format(v[i]) if(k[i]==1) else "{}黑纸 ".format(v[i]),end="")







在这里插入图片描述
以上输出写的不太优雅,有更好的方法请告知。

C语言写法

发布了51 篇原创文章 · 获赞 5 · 访问量 2138

猜你喜欢

转载自blog.csdn.net/weixin_44659084/article/details/103339718