- 题目
两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比
,请编程序找出三队赛手的名单。
方法一:
这个方法感觉十分的巧妙,你悟了吗????
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
## 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。
## 有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
dict = {1:'a', 2:'b', 3:'c'}
for x in range(1,4):
if (x == 1) or (x == 3):
continue
for y in range(1,4):
for z in range(1,4):
if (z == 3):
continue
if (x * y * z == 6):
print("x--{0},y--{1},z--{2}".format(dict[x],dict[y],dict[z]))
结果如下:
方法二:
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
list_one,list_two,list_target = ['a','b','c'],['x','y','z'],[]
for elem_one in list_one:
for elem_two in list_two:
if elem_one == 'a' and elem_two == 'x':
continue
if elem_one == 'c':
if elem_two == 'z' or elem_two == 'x':
continue
list_target.append(elem_one + elem_two)
for target1 in list_target:
for target2 in list_target:
for target3 in list_target:
str = target1 + target2 + target3
ls = list(str)
ls.sort()
str = ''.join(ls)
if (str == "abcxyz"):
print(target1,target2,target3)
exit(0)
运行结果如下: