#计算阶乘的3种方法
#采用普通函数1: 非递归
def fx1(n):
result = n
for i in range(1,n):
result = result * i
return result
#采用普通函数2: 非递归
def fx2(n):
i = 1
j = 0
while j < n:
i = i*(j+1)
j = j+1
return i
#采用递归算法:
def fx3(n):
if n == 1:
return 1
else:
return n*fx3(n-1)
number = int(input('请输入一个整数:'))
result2 = fx3(number)
print "%d 的阶乘为: %d" % (number,result2)
输入5,运行结果如下:
>>>
请输入一个整数:5
5 的阶乘为: 120
Warning: include(/www/wwwroot/fengjinwei.com/wp-content/themes/fj/relatepost.php): failed to open stream: No such file or directory in /www/wwwroot/fengjinwei.com/wp-content/themes/fj/single.php on line 97
Warning: include(): Failed opening '/www/wwwroot/fengjinwei.com/wp-content/themes/fj/relatepost.php' for inclusion (include_path='.:') in /www/wwwroot/fengjinwei.com/wp-content/themes/fj/single.php on line 97
最新评论