python基础——实现阶乘的几种方法

实现一个整数的阶乘
方法一:循环求解
num= int(input())
count = 1
for i in range(1, num+1):
	count *= i
print(count)
方法二:函数式编程求解
from functools import reduce
num = int(input())
print(reduce(lambda x,y:x*y, range(1, num+1)))
方法三:递归求解
def func(x):
	if x == 1:
		return 1
	else:
		return x * func(x-1)
print(func(int(input())))
方法四:python内置阶乘函数求解
from math import factorial
print(factorial(int(input())))
发布了37 篇原创文章 · 获赞 2 · 访问量 951

猜你喜欢

转载自blog.csdn.net/zzrs_xssh/article/details/103738838