递归求阶乘-成都快上网建站

递归求阶乘

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://kswjz.com/article/jdicos.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流