问题:两个乒乓球队进行比赛,各出三个。甲队为a, b, c三个,乙队为x, y, z三个。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x, z比,请编程找出三队赛手的名单。
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : Ma Yi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2020-06-19 # Name : demo022 # Software : PyCharm # Note : 两个乒乓球队进行比赛,各出三个。甲队为a, b, c三个,乙队为x, y, z三个。已抽签决定比赛名单。有人向队员打听 # 比赛的名单。a说他不和x比,c说他不和x, z比,请编程找出三队赛手的名单。 # 入口函数 if __name__ == '__main__': # 让a, b, c三人分别遍历x, y, z,并将重复出现的对阵及题目提供的对阵给去除即可。 for a in ["x", "y", "z"]: for b in ["x", "y", "z"]: for c in ["x", "y", "z"]: if a != b and b != c and c != a and a != "x" and c != "x" and c != "z": # 对阵名单 print("a对阵%s, b对阵%s, c对阵%s" % (a, b, c))
运行结果:
"C:Program FilesPython35-32python.exe" F:/朱粮梁/1.项目-Python/Python/MyPython/练习代码/2020/202006/20200618/demo022.py a对阵z, b对阵x, c对阵y