递归与迭代之间的区别 [英] Difference between recursion and iteration

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

问题描述

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

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

MW: 迭代-1:迭代或重复的动作或过程:a:重复操作序列产生结果的过程,其结果逐渐接近所需结果b:将计算机指令序列重复指定次数或直到满足条件为止

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

递归-3:一种计算机编程技术,涉及使用一个过程,子例程,函数或算法,该过程会自我调用一次或多次,直到满足指定条件为止,此时,每次重复的其余部分将从上一次开始进行处理.一个被称为第一个

Recursion - 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

推荐答案

我们可以将递归基本相同:a <本身就定义了href ="http://mathworld.wolfram.com/RecursiveFunction.html" rel ="nofollow noreferrer">递归过程.迭代过程使用循环语句重复代码块.但是,递归过程是采用非恒定过程的过程(例如 O (n)或O(lg(n))空间)执行,而迭代过程则占用O(1)(恒定)空间.

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表示法与迭代类似:

Pi标记.类似于如何将某些(数学)递归公式重写为迭代公式,某些(但不是全部)递归过程具有迭代等效项.通过在您自己的数据结构中跟踪部分结果,而不是使用函数调用堆栈,可以将所有递归过程转换为迭代过程.

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天全站免登陆