topic
Use the equivalent algorithm to solve the following problem: 4 people A, B, C, and D participate in the 100-meter race, and audiences A, B, and C predict the rankings of the race as follows.
A: C first, B second.
B: C is second and D is third.
C: A is second and D is fourth.
After the game, it is found that A, B, and C each predicted that the situation is 50/50. How about the actual ranking (assuming there is no tied ranking)?
the code
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}!')
result
in conclusion
It can be seen from the running results that the final ranking of the competition is A second, B fourth, C first, and D third.