递归和迭代 [英] Recursion and Iteration

查看:281
本文介绍了递归和迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?这些是一样的吗?如果没有,有人可以给我一个例子吗?



MW:
迭代 - 1:迭代或重复的动作或过程:a:a重复一系列操作产生结果的过程更接近于期望的结果b:计算机指令序列重复指定的次数或直到满足条件



Recusion - 3:一种计算机编程技术,涉及使用一个过程调试自身一次或多次的过程,子程序,函数或算法,直到满足指定条件,此时每次重复的其余部分从最后一次处理一个叫做第一个

解决方案

我们可以从类似于迭代:





Pi符号。类似于一些(数学)递归公式可以重写为迭代公式,一些(但不是全部)递归过程具有迭代等价物。通过跟踪您自己的数据结构中的部分结果,而不是使用函数调用堆栈,可以将所有递归过程转换为迭代过程。


What is the difference? Are these the same? If not, can someone please give me an example?

MW: Iteration - 1 : the action or a process of iterating or repeating: as a : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result b : the repetition of a sequence of computer instructions a specified number of times or until a condition is met

Recusion - 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first

解决方案

We can distinguish (as is done in SICP) recursive and iterative procedures from recursive and iterative processes. The former are as your definition describes, where recursion is basically the same as mathematical recursion: a recursive procedure is defined in terms of itself. An iterative procedure repeats a block of code with a loop statement. A recursive process, however, is one that takes non-constant (e.g. O(n) or O(lg(n)) space) to execute, while an iterative process takes O(1) (constant) space.

For mathematical examples, the Fibonacci numbers are defined recursively:

Sigma notation is analogous to iteration:

as is Pi notation. Similar to how some (mathematical) recursive formulae can be rewritten as iterative ones, some (but not all) recursive processes have iterative equivalents. All recursive procedures can be transformed into iterative ones by keeping track of partial results in your own data structure, rather than using the function call stack.

这篇关于递归和迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆