1.王教授是哪里人
甲说:王教授不是苏州人是上海人
乙说:王教授不是上海人,是苏州人
丙说:王教授既不是上海人也不是苏州人
这三人中一人说的全对,一人对一半,另一人说的全不对,试求王教授是哪里人。
答,设p:王教授是苏州人
q:王教授是上海人
r:王教授是杭州人
理论:分别写出甲的判断,乙的判断,丙的判断的命题变项
甲的判断为:Q=非p并q
乙的判断为:M=p并非q
丙的判断为:B=非q并非r
甲的判断全对: B1=(not p) and q
甲的判断一半对:B2=((not p) and (not q)) or (p and q)
甲的判断全错: B3=p and (not q)
乙的判断全对:C1=p and (not q)
乙的判断一半对:C2=(p and q) or ((not p) and (not q))
乙的判断全错:C3=(not p) and q
丙的判断全对:D1=(not q) and (not r)
丙的判断一半对:D2=((not q) and r) or (q and (not r))
丙的判断全错:D3=q and r
【代码实现如下】
for p in range(2):#分别把0,1赋值给p
for q in range(2):#分别把0,1赋值给q
for r in range(2):#分别把0,1赋值给r
if (p==1 and q==1):
continue
if (p==1 and r==1):
continue
if (q==1 and r==1):
continue
if (q==1 and p==1 and r==1):
continue
Q=(not p) and q
M=(not q) and p
B=(not q) and (not r)
B1=(not p) and q
B2=((not p) and (not q)) or (p and q)
B3=p and (not q)
C1=p and (not q)
C2=(p and q) or ((not p) and (not q))
C3=(not p) and q
D1=(not q) and (not r)
D2=((not q) and r) or (q and (not r))
D3=q and r
E=(B1 and C2 and D3)or(B1 and C3 and D2)or(B2 and C1 and D3)or(B2 and C3 and D1)or(B3 and C1 and D2)or(B3 and C2 and D1)#对E进行赋值,‘一人对,一人错,一人半对半错’为T
if E ==1:
print("%d,%d,%d Q=%d,M=%d,E=%d,B=%d"%(p,q,r,Q,M,E,B))#分别得出当E为真时的p,q,r,Q,M,E,B的真值
if p==1:
print("王教授是苏州人")
if q==1:
print("王教授是上海人")
if r==1:
print("王教授是杭州人")
if ((not p)==1 and q==0) or ((not p)==0 and q==1):
print("其中甲是对一半的")
if (p==0 and (not q)==1)or (p==1 and (not q)==0):
print("其中乙是对一半的")
if ((not q)==1 and(not r)==0)or((not q)==0 and(not r)==1):
print("其中丙是对一半的")#写出哪一位是对一半的
运行得出结果为:0,1,0 Q=1,M=0,E=1,B=0
王教授是上海人
其中丙是对一半的