Python 一行代码系列——计算阶乘
代码:
__import__('sys').set_int_max_str_digits(100000000);print(__import__('functools').reduce((lambda x, y: x * y), range(1, int(input()) + 1)))
原理:
__import__('sys').set_int_max_str_digits(100000000) # 不然数字大了会报错,如果数字不大可以删去
print( # Step 3: 输出
__import__('functools').reduce(
(lambda x, y: x * y), # Step 2: 累乘
range(1, int(input()) + 1) # Step 1: 输入 n=5 返回 [1,2,3,4,5]
)
)