• 题目
    两个乒乓球队进行比赛,各出三人。甲队为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)

运行结果如下:
在这里插入图片描述

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐