王小红,李强,丁金生三名同学被选进班委会。该班的甲乙丙三名同学预言如下:
甲:王小红为班长,李强为生活委员
乙:丁金生为班长,王小红为生活委员
丙:李强为班长,王小红为学习委员
其中甲乙丙三人都恰好猜对一半,问以上三人各任何职。
p1: 王小红是班长
p2:丁金生是班长
p3:李强是生活委员
q1:王小红是生活委员
q2 :李强是班长
q3:王小红是学习委员
Q=((not p1) and p2) or (p1 and (not p2))
F=((not p3) and q1) or (p3 and (not q1))
S=((not q2) and q3) or (q2 and (not q3))
其中E = Q并F并S 为真
【代码如下】
#分别给各个命题变项赋值
for p1 in range(2):
for p2 in range(2):
for p3 in range(2):
for q1 in range(2):
for q2 in range(2):
for q3 in range(2):
#用if条件语句列出可能的情况使程序继续运行
if (p1==1 and p3==1):
continue
if (p1==1 and q1==1):
continue
if (p1==1 and q3==1):
continue
if (p2==1 and q1==1):
continue
if (p2==1 and q2==1):
continue
a=(not p1) and p2
b=p1 and (not p2)
c=(not p3) and q1
d=p3 and (not q1)
e=(not q2) and q3
f=q2 and (not q3)#分别单独列出甲乙丙各对一半的情况
Q=((not p1) and p2) or (p1 and (not p2))
F=((not p3) and q1) or (p3 and (not q1))
S=((not q2) and q3) or (q2 and (not q3))#列出甲乙丙各对一半的可能情况
E = Q and F and S
if E ==1:
print("%d,%d,%d,%d,%d,%d"%(a,b,c,d,e,f))
if p1==1:
print("王小红是班长")
if p2==1:
print("丁金生是班长")
if p3==1:
print("李强是生活委员")
if q1==1:
print("王小红是生活委员")
if q2==1:
print("李强是班长")
if q3==1:
print("王小红是学习委员")#列出各个条件为真时的结果
【 运行结果】
丁金生是班长
李强是生活委员
王小红是学习委员