正如已经指出的那样,递归是函数调用自身的时候.这里使用说明factorial,在factorial(5)数学上评估值5 * 4 * 3 * 2 * 1.
public int factorial(int x) {
if (x == 1) return 1;
else return x * factorial (x - 1);
}
// Try routine
factorial(3);
Run Code Online (Sandbox Code Playgroud)
评估为
factorial(1) = 1 = 1
factorial(2) = 2 * factoral(1) = 2 * 1
factorial(3) = 3 * (2 * factorial(2) * (factorial(1)) = 3 * 2 * 1
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12182 次 |
| 最近记录: |