题目描述: 某公司要从赵、钱、孙、李、周五名新毕业的大学生中选派一些人出国学习. 选派必须满足以下条件:
(1) 若赵去, 则钱也去
(2) 李、周两人中必有一人去
(3) 钱、孙两人中去且仅去一人
(4) 孙、李两人同去或同不去
(5) 若周去, 则赵、钱也同去
分析该公司如何选派他们出国.
代码如下:
#派人出国学习 p:派赵去 q:派钱去 r:派孙去 m:派李去 n:派周去
for p in range(0,2):
for q in range(0,2):
for r in range(0,2):
for m in range(0,2):
for n in range(0,2):
n1 = ((p == 1 and n == 1) or (p == 0 and n == 0))
n2 = (m + n >= 1)
n3 = (q + r == 1)
n4 = ((r + m == 0) or (r + m == 2))
n5 = ((n == 1 and p == 1 and q == 1) or (n == 0 and p == 0 and q == 0))
if n1 and n2 and n3 and n4 and n5 == True:
print("p:{} q:{} r:{} m:{} n:{}".format(p,q,r,m,n))
运行如下:
p:0 q:0 r:1 m:1 n:0
p:1 q:1 r:0 m:0 n:1
故有两种派遣方法:
①派孙、李出国学习
②派赵、钱、周出国学习