递归方法求n!的实现如下:

def fact(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * fact(n-1)

# 测试
n = int(input("请输入一个正整数:"))
result = fact(n)
print("{}! = {}".format(n, result))

运行结果示例:

请输入一个正整数:5
5! = 120
题目:利用递归方法求n!。 1程序分析:递归公式:fn=fn_14!

原文地址: http://www.cveoy.top/t/topic/izuZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录