大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

递归求阶乘

def factorial(n):
    result = n
    for i in range(1,  n):
        result *= i
    return result
number = int(input("请输入一个正整数:"))

result = factorial(number)
print("%d的阶乘是:%d" %(number, result))

-----
def factorial(n):
    if n == 1:
        return 1
    else:
        return n* factorial(n - 1)
number = int(input("请输入一个正整数:"))

result = factorial(number)
print("%d的阶乘是:%d" %(number, result))

斐波那契数列

def f(n):
    if n == 1:
        ret = 0
    elif n == 2:
        ret = 1
    else:
        ret = f(n-1)+f(n-2)
    return ret
print(f(4))
def f(n):
    if n == 1:
        return 0
    elif n == 2:
        return 1
    return f(n-1)+f(n-2)
print(f(5))

网站栏目:递归求阶乘
文章来源:http://dzwzjz.com/article/jdicos.html
在线咨询
服务热线
服务热线:028-86922220
TOP