我一直在试图弄清这个递归方法的堆栈如何.
public class Apples {
public static void main (String [] args) {
q1(5);
}
public static int q1 (int x) {
if (x < 1) {
System.out.println(x);
return 1;
}
int a = 3 + q1(x / 2);
int b = 2 * q1(x - 2) + 1;
System.out.println(x + ";" + a + ";" + b);
return a + b;
}
}
但到目前为止,我只认为堆栈根据x / 2增长:
x=0 returns 1;
x=1 a=4 b=3 returns 7;
x=2 a=10 b=3 returns 13;
x=5 a=16 b=9 returns 19;
这显然既不真实也不完整.请帮助我了解堆栈如何建立.