- 递归:
# 阶乘-递归
def fact_iter(n):
if n ==1:
return 1
else:
return n * fact_iter(n-1)
n = int(input('输入一个数字: '))
result = fact_iter(n)
print('结果是: ',result)
- 迭代:
# 阶乘-迭代
def fact_recur(n):
result = n
for i in range(1,n):
result *= i
return result
n = int(input('输入一个数字: '))
result = fact_recur(n)
print('结果是: ',result)
网友评论