Python编程实现以下问题

编程实现:在一次编位北赛中,甲乙丙丁分别得了前四名,好奇人士分别问他们是几名,回答如下:甲说:“第一名是丙。第二名是丁乙说:“第三名是甲。第四名是丁丙说:“第一名是乙第三是丙。"丁说“第四名是丙。
第二名是乙”他们都只说对了一半。求真实名次提示:random中前shuffle0可对列表中的元素随机重排位置。若x是个列表,randon shuffle(d)表示对列表元素随机重排位置


编程实现:
  在一次排位比赛中,甲乙丙丁分别得了前四名,好奇人士分别问他们是几名,回答如下——

甲说:“第一名是丙,第二名是丁。”
乙说:“第三名是甲,第四名是丁。”
丙说:“第一名是乙,第三名是丙。"
丁说:“第四名是丙,第二名是乙。”

  他们都只说对了一半。

求真实名次提示:
random中的shuffle()方法可对列表中的元素随机重排位置(打乱、洗牌)。若x是个列表,randon.shuffle(x)表示对列表元素随机重排位置。



  可以用最笨的“暴力求解”,4层for循环罗列所有合法“甲乙丙丁”排名组合,据“甲乙丙丁”四人描述,剔除不符组合,最后水落石出,答案自现。



Python 代码

#!/sur/bin/nve python
# coding: utf-8

# 暴力求解
char = '甲乙丙丁'

for i in char:
    for j in char:
        for m in char:
            for n in char:
                sortchar = f"{i}{j}{m}{n}"

                if len(set(sortchar)) < 4:
                    continue
                elif sortchar[0] == '丙' and sortchar[1] == '丁':
                    continue
                elif sortchar[2] == '甲' and sortchar[3] == '丁':
                    continue
                elif sortchar[0] == '乙' and sortchar[2] == '丙':
                    continue
                elif sortchar[1] == '乙' and sortchar[3] == '丙':
                    continue

                if sortchar[2] == '甲' and sortchar[1] != '丙':
                    print(sortchar)

代码运行效果截屏图片

img