我在factorial这样的例子中“学习”了递归,在factorial中递归调用返回一个等价的数学函数,通过每次返回进行计算。你知道吗
这似乎比使用out参数来存储向下的计算更加优雅和可证明。你知道吗
python中这两种方法的优缺点是什么?你知道吗
编辑:
我最初的问题是含糊不清和不准确的,所以我在下面添加了不同风格的例子。你知道吗
def factorial_1(x):
def helpFactorial_1(x,out):
if x==1:
return out
else:
return helpFactorial_1(x-1,x*out)
return helpFactorial_1 (x,1)
VS
def factorial_2(x):
if x==1:
return 1
else:
return x*factorial_2(x-1)
目前没有回答
相关问题 更多 >
编程相关推荐