题目
用等值演算法解决下面问题:A、B、C、D4人参加百米竞赛,观众甲、乙、丙预测比赛的名次如下。
甲:C第一、B第二。
乙:C第二、D第三。
丙:A第二、D第四。
比赛结束后发现甲、乙、丙每人预测的情况都是各对一半,试问实际名次如何(假设无并列名次)?
代码
1. from itertools import permutations
2.
3. def check(p):
4. a,b,c,d=p
5. 甲 = (c==1 and not b==2) or (not c==1 and b==2)
6. 乙 = (c==2 and not d==3) or (not c==2 and d==3)
7. 丙 = (a==2 and not d==4) or (not a==2 and d==4)
8. return 甲 and 乙 and 丙
9.
10. for p in permutations([1,2,3,4],4):
11. try:
12. flag = check(p)
13. assert flag,f'{p} is wrong!'
14. except Exception as e:
15. print(e)
16. finally:
17. if not flag:
18. continue
19. else:
20. print(f'find it!!! the answer is {p}!')
结果
结论
由运行结果可见,比赛的最终名次为A第二、B第四、C第一、D第三。