zoukankan      html  css  js  c++  java
  • Python【每日一问】28

    问:

    【基础题】:求 1+2!+3!+...+20! 的和

    【提高题】:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。 有人向队员打听比赛的名单。 a 说他不和 x 比, c 说他不和 x,z 比,请找出三队赛手的名单

    答:

    基础题:求 1+2!+3!+...+20! 的和

    方法1:

    def factorial(n):
        if n == 0 or n == 1:
            return 1
        else:
            return factorial(n - 1) * n
    ​
    ​
    my_sum = sum(factorial(i) for i in range(1, 21))
    print(f"1! + 2! + 3! + 4! + 5! + ... + 20!={my_sum}")

    方法2:

    def recursion(n): #'定义递归函数实现求阶乘功能'
        if n == 1:
            return 1
        else:
            return n*recursion(n-1)
    ​
    ​
    list_recursion = []  # 定义一个空的列表,将调用递归函数生成的阶乘值追加到列表
    for i in range(1,21):
        list_recursion.append(recursion(i))  # 将调用递归函数生成的阶乘值追加到列表
    ​
    ​
    print(sum(list_recursion))

    方法3:

    def recursion(n): #'定义递归函数实现求阶乘功能'
        if n == 1:
            return 1
        else:
            return n*recursion(n-1)
    ​
    ​
    Sum = 0
    for i in range(1, 21):
        Sum += recursion(i)
    print(Sum) 

    方法4:

    def factorial(num):
        if num == 1:
            return 1
        else:
            return num * factorial(num - 1)
    ​
    ​
    def sum_factorial():
        iteration = int(input("Input the max iteration:"))
        result = 0
        for i in range(1, iteration+1):
            result += factorial(i)
        print("Alright, we catch the result:{}".format(result))
    
    ​
    ​
    if __name__ == '__main__':
        sum_factorial()

    方法5:

    def factorial(n):
        f = 1
        for i in range(1, n+1):
            f = f * i
        return f
    ​
    num = int(input("请输入一个数字:"))
    sum = 0
    if num < 0:
        print("抱歉,负数没有阶乘")
    elif num == 0:
        print("0的阶乘为1")
    else:
        for i in range(1, num+1):
            sum += factorial(i)
        print(sum)

    方法6:

    def factorial(number):
        start = 1
        fact = 1
        while start <= number:
            yield fact
            start += 1
            fact *= start
    ​
    ​
    fact = factorial(20)
    sum = 0
    ​
    for i in fact:
        sum += i
    print(sum)

     

    提高题:两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。 有人向队员打听比赛的名单。 a 说他不和 x 比, c 说他不和 x,z 比,请找出三队赛手的名单。

     

    from itertools import permutations
    ​
    team_a = ["a", "b", "c"]
    team_b = ["x", "y", "z"]
    ​
    possible_match_lists = []
    ​
    for possible_team_b_sequence in list(permutations(team_b)):
        possible_match_lists.append(dict(zip(team_a, possible_team_b_sequence)))
    ​
    final_match_lists = list(filter(lambda x: x["a"] != "x" and 
                                    x["c"] != "x" and 
                                    x["c"] != "z", possible_match_lists))
    ​
    for index, match_list in enumerate(final_match_lists):
        print(f"solution{index+1}")
        for competitiors in match_list.items():
            print(" vs ".join(competitiors))
  • 相关阅读:
    WAMP环境下访问PHP提示下载PHP文件
    安装CMS遇到php5.3的问题
    Win2003打不开https的问题
    查看服务器硬件信息
    添加删除程序无法安装IIS 提示没法加载模块
    Spring Autowiring by AutoDetect
    Spring Autowiring by Constructor
    Spring Autowiring by Name
    Spring Autowiring by Type
    Spring Auto-Wiring Beans
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/10895119.html
Copyright © 2011-2022 走看看