简单的递归示例 - 请帮助我理解递归
public static int triple(int n)
{
if (n == 0)
return 0;
else
total = 3 + triple(n-1);
System.out.println(total);
return total;
}
好吧,我已经得到了这个简单的递归示例,但我似乎无法理解,我希望有人能好心地引导我逐个循环地了解程序如何获取其输出。
这是我认为会发生的事情。可以说n=5
因此,程序循环并命中 total = 3 + Triple(5-1)
我认为等于 7..这是错误的 程序打印
3
6
9
12
15
所以...然后我认为在打印总数之前必须再次运行三重...我相信它确实如此,但我根本不明白它是如何得出总数的。
因为它看起来像这样:
3 + triple(4)
3 + triple(3)
3 + triple(2)
3 + triple(1)
=3
有人可以解释一下吗,我很迷失!
public static int triple(int n)
{
if (n == 0)
return 0;
else
total = 3 + triple(n-1);
System.out.println(total);
return total;
}
Ok, so I've got this simple recusion example that I just cant seem to grasp, I was hoping someone would be kind enough to walk me through cycle by cycle of how the program gets its output.
Here is what I thought would happen. Lets say n=5
So, the program cycles and hits total = 3 + triple(5-1)
which i would think would be equal to 7.. which is wrong
the program prints
3
6
9
12
15
So... then I thought triple must run through again before printing the total... which I believe it does but I just don't understand how it comes to its totals at all then.
Because it would just look like this :
3 + triple(4)
3 + triple(3)
3 + triple(2)
3 + triple(1)
=3
Can someone explain please, as you can I am very lost!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您对三元组(n-1)和(n-1)感到困惑。它们是不同的东西,所以你不能只将值分配给三元组(n-1)中的n,然后将其添加到3
You got confused between triple(n-1) and (n-1). They are different thing, so you cannot just assign the value to n inside triple(n-1) and then add it to 3
你的解释有点错误。它更像是这样的:
现在假设
triple(0)
、triple(1)
等都是单独的变量,并求解triple(5)< /code> 通过努力向上。
You're interpreting it slightly wrong. It's more like this:
Now imagine that
triple(0)
,triple(1)
, etc. are all individual variables, and solve fortriple(5)
by working your way up.那么它不会先减到零(减去 1),然后为每个加 3(0 3 6 等)。
这是我得到的输出:
它所做的是从每个枚举中的 n 中减去 1,然后将 3 添加到现在的 0-5
So wouldn't it work its way down to zero doing (by subtracting 1) then add 3 to each (0 3 6, etc).
This is the output I'm getting:
What it's doing is subtracting one from n each enumeration in, then adding 3 to the now 0-5
您的输出应如下所示:
这是因为 Triple(n) 会在打印消息之前调用 Triple(n-1)。所以你的三重(5)消息将最后打印。
Your output should be read as follows:
It is because triple(n) would invoke triple(n-1) before printing out the message. So your triple(5) message will be printed last.
当执行到达该点时,三元组方法从头开始再次执行。一旦返回,执行将在下一行继续。这递归地发生。
所以执行顺序是这样的:
请注意,定义的函数只是将输入乘以 3,并打印每个倍数的结果。
When the execution hits that point, the triple method begins executing again from the start. Once it returns, execution will then resume at the next line. This happens recursively.
So the order of execution is something like:
Note the function as defined just multiplies the input by 3, and prints the result at each multiple.
忽略 println(return) 它只是为了理解目的。这就是我将其分解以最终很好地掌握递归函数/方法的方式。
感谢大家帮助理解这一点。我没有做的事情是记住每个三元组(n-1)返回其自己的值,然后将其计算到上面的调用中。
再次感谢!
Disregard the println(return) its just for understanding purposes. This is how i broke it down to finally get a good grasp on recursive functions/methods.
Thank you all for you help in understanding this. The thing I was not doing was remembering that each triple(n-1) returned its own value that was then calculated into the call above it.
THANKS AGAIN!