《练习100》21~25

题目21

# 两个乒乓球队,进行比赛,各处三人,甲队为abc,乙队为xyz。a不和x比,c不和xz比。编程找出三队选手名单
team_a = ['a','b','c']

# 站在乙队的角度,从甲队中挑选对手
for i in team_a:        # 为x挑选对手
    for j in team_a:    # 为y挑选对手
        for k in team_a:# 为z挑选对手
            # 首先每位选手的对手不能相同
            if i == j or i == k or j == k:
                continue 
            
            # 然后根据选手喜好挑选
            if i == 'a' or i == 'c' or k == 'c':
                continue
            
            print(f'x vs {i}, y vs {j}, z vs {k}')

team_b = ['x', 'y', 'z']
# 站在甲队的角度,从乙队中为甲队队员挑选选手
for i in team_b:        # 为a挑选对手
    for j in team_b:    # 为b挑选对手
        for k in team_b:# 为c挑选对手
            # 对手不能是同一人
            if i == j or i == k or j == k:
                continue 
            
            if i == 'x' or k == 'x' or k == 'z':
                continue
            print(f'a vs {i}, b vs {j}, c vs {k}')  

# 先生成所有对局可能,然后依次为甲队的三个选手挑选比赛
match=[]
for i in team_a:
    for j in team_b:
        match.append((i,j))

#挑选三场比赛
for i,j in match:
    for k,l in match:
        for m,n in match:
            # 一个选手只能参加一场
            if i == k or i == m or k == m:
                continue
            if j == l or j == n or l == n:
                continue 
            
            # 依次为甲队的三位选手挑选比赛
            if i != 'a' or k != 'b' or m != 'c':
                continue
            
            # a不和x比,c不和xz比
            if i == "a" and j == 'x':
                continue
            
            if m == 'c' and (n == 'x' or n == 'z'):
                continue
            
            print(f'{i} vs {j}, {k} vs {l}, {m} vs {n}') 

题目22

# 打印一个棱形
#   *
#  ***
# *****
#*******
# *****
#  ***
#   *

# 规则 空白和*的数量和为7,*数量为奇数,总的行数也是7

star_cnt = 1
for i in range(7):
    half_space = (7 - star_cnt)//2
    print((' ' * half_space) + ("*" * star_cnt))
    if i < 7//2:
        star_cnt += 2
    else:
        star_cnt -= 2

题目23

# 有一个分数序列,2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列前20项之和
# 分开分析:
# 次数:1,2,3,4,5,6
# 分子:2,3,5,8,13,21, ...
# 分母:1,2,3,5, 8,13, ...

# 下列式子中出现的n-1,n-2 均为下角标
# 分子,起始f1 = 2, f2 = 3, f3 = f2 + f1, fn = fn-1 + fn-2 (n >2)
# 分子:f1 = 1, f2 = 2, f3 = f1 + f2 = 3, f4 = f3+f2 = 5, f5 = f4 + f3 = 8, fn = fn-1 + fn-2 (n>2)

def get_num(f1: int, f2: int, n: int):
    if n == 1:
        return f1
    if n == 2:
        return f2

    for i in range(3, n+1):
        f1, f2 = f2, f1+f2

    return f2


# 一起分析:
# 后一项的分子:是前一项的分子+分母
# 后一项的分母,是前一项的分子

def get_num1(n: int):
    f1 = 2
    f2 = 1
    sum = f1/f2
    if n == 1:
        return sum
    for i in range(1, n):
        f2, f1 = f1, f1+f2
        sum += f1/f2
    return sum


while True:
    try:
        n = int(input("请输入重复的次数:\n"))
        sum = 0.0
        for i in range(1, n+1):
            sum += get_num(2, 3, i)/get_num(1, 2, i)
            if i == n:
                print(f' {get_num(2,3,i)}/{get_num(1,2,i)}', end='')
            else:
                print(f' {get_num(2,3,i)}/{get_num(1,2,i)} +', end='')
        print(" = ", sum)
        print(get_num1(n))
    except ValueError:
        print("请输入一个整数")
        break

题目24

# 求1+2!+3!+4!+...+20!之和
sum = 0
for i in range(1, 21):
    product = 1
    for j in range(1, i+1):
        product *= j
    sum += product
    print(f"i = {i}, product = {product}")
print("1+2!+3!+4!+...+20! =",sum)

题目25

# 利用递归的方法求5!
# 5!= 1*2*3*4*5
def fact(j):
    if j == 0:
        return 1
    else:
        return  j*fact(j-1)

for i in  range(5+1):
    print(f"{i}! = {fact(i)}")

你可能感兴趣的:(Python学习笔记,python,开发语言,学习)