正在加载图片...
Iteration vs.Recursion Iteration and recursion are somewhat related ● Converting iteration to recursion is formulaic,but converting recursion to iteration can be more tricky Iterative Recursive def fact_iter(n): deffact(n): total,k=1,1 ifn==0: whilek <n: return 1 total,k total*k,k+1 else: return total return n*fact(n-1)】 m n!=Ik nn(n-1)!otiherwise if n 0 k=1 Names:n,total,k,fact_iter Names:n,factIteration vs. Recursion ● Iteration and recursion are somewhat related ● Converting iteration to recursion is formulaic, but converting recursion to iteration can be more tricky def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Iterative Recursive Names: n, total, k, fact_iter Names: n, fact
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有