Four rules of recursion

来源:互联网 发布:手机号码采集软件 编辑:程序博客网 时间:2024/05/21 06:22
1. Base cases. You must always have some base cases, which can be solved without recursion.

2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case.

3. Design rule. Assume that all the recursive calls work.

4. Compound interest rule(合成效益法则). Never duplicate work by solving the same instance of a problem in separate recursive calls.

 

在写递归时,这4个法则需要注意。

关于第4个法则,比如用递归来算斐波那契数列,那么会有大量的重复性工作,会大大影响程序运行效率。

 

 


Data Structures and Algorithm Analysis in C

2015.7.11

0 0