java 简单的递归示例 - 请帮助我理解递归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5533895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:34:18  来源:igfitidea点击:

simple recursive example - please help me understand recursion

javamathrecursion

提问by John

    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=5So, 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

这是我认为会发生的事情。让我们这么说吧n=5total = 3 + triple(5-1)我认为程序循环和点击数等于 7 .. 这是程序打印的错误

3
6
9
12
15

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!

有人可以解释一下吗,我很迷茫!

回答by BlueRaja - Danny Pflughoeft

You're interpreting it slightly wrong. It's more like this:

你对它的解释有点错误。更像是这样:

triple(5) = 3 + triple(4)
triple(4) = 3 + triple(3)
triple(3) = 3 + triple(2)
triple(2) = 3 + triple(1)
triple(1) = 3 + triple(0)
triple(0) = 0

Now imagine that triple(0), triple(1), etc. are all individual variables, and solve for triple(5)by working your way up.

现在想象一下triple(0)triple(1)等都是独立的变量,并解决triple(5)通过自己的方式工作了。

回答by Ben

So wouldn't it work its way down to zero doing (by subtracting 1) then add 3 to each (0 3 6, etc).

因此,它不会降低到零(通过减去 1)然后将 3 添加到每个(0 3 6 等)。

This is the output I'm getting:

这是我得到的输出:

n:5
n:4
n:3
n:2
n:1
n:0
total:3
total:6
total:9
total:12
total:15

What it's doing is subtracting one from n each enumeration in, then adding 3 to the now 0-5

它所做的是从每个枚举中的 n 中减去 1,然后将 3 添加到现在的 0-5

回答by Tommy Siu

Your output should be read as follows:

您的输出应如下所示:

3 = triple(1) = 3+triple(0)
6 = triple(2) = 3+triple(1)
9 = triple(3) = 3+triple(2)
12 = triple(4) = 3+triple(3)
15 = triple(5) = 3+triple(4)

It is because triple(n) would invoke triple(n-1) beforeprinting out the message. So your triple(5) message will be printed last.

这是因为triple(n) 会打印消息之前调用triple(n-1) 。因此,您的三重 (5) 消息将最后打印。

回答by John

public static int triple(int n)
{

if (n == 0)
return 0;
else
return 3 + triple(n-1);
System.out.println(return);
}

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.

忽略 println(return) 它只是为了理解目的。这就是我如何分解它以最终很好地掌握递归函数/方法。

triple(3)
return 3 + triple(3-1)_is_6<---- (return = 9)<--
println(return)

    triple(2)
    return 3 + triple(2-1)_is_3<-- (return = 6)<----
    println(return);

              triple(1)
              return 3 + triple(1-1)_is_0<---- (return = 3)<--
              println(return)

                       triple(0)
                       return 0; is 0 (return = 0)<----
                       (no println for n==0)

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.

感谢大家帮助理解这一点。我没有做的事情是记住每个三元组(n-1)返回自己的值,然后计算到它上面的调用中。

THANKS AGAIN!

再次感谢!

回答by Amartya Raghav

triple(0) = 0
triple(1) = 3 + triple(0) i.e. 3+0=3
triple(2) = 3 + triple(1) i.e. 3+3=6
triple(3) = 3 + triple(2) i.e. 3+6=9
triple(4) = 3 + triple(3) i.e. 3+9=12
triple(5) = 3 + triple(4) i.e. 3+12=15

回答by MidoriXie

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

您在三重(n-1)和(n-1)之间感到困惑。它们是不同的东西,所以你不能只是将值分配给三元组(n-1)中的 n 然后将其添加到 3

回答by davmac

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:

所以执行的顺序是这样的:

  1. if (n == 0) // n == 5 at this point, condition is false
  2. total = 3 + triple(n-1) // we must calculate triple(4)
  3. if (n == 0) // n == 4 now.
  4. total = 3 + triple(n-1) // we must calculate triple(3)
  5. ... etc, until n does == 0:
  6. if (n == 0) // true!
  7. return 0 // returns 0
  8. total = 3 + 0 // 0 comes from triple(n-1), which just returned 0
  9. System.out.println(total); // prints 3
  10. return total; // returns 3
  11. total = 3 + 3; // again 3 comes from triple(n-1), where n==2
  12. System.out.println(total); // prints 6 this time
  13. ... and so on.
  1. if (n == 0) // 此时 n == 5,条件为假
  2. total = 3 +triple(n-1) // 我们必须计算triple(4)
  3. if (n == 0) // 现在 n == 4。
  4. 总计 = 3 + 三元组(n-1) // 我们必须计算三元组(3)
  5. ... 等等,直到 n 确实 == 0:
  6. if (n == 0) // 真!
  7. 返回 0 // 返回 0
  8. total = 3 + 0 // 0 来自三元组(n-1),它刚返回 0
  9. System.out.println(总); // 打印 3
  10. 总回报;// 返回 3
  11. 总计 = 3 + 3;// 同样 3 来自三元组(n-1),其中 n==2
  12. System.out.println(总); // 这次打印 6
  13. ... 等等。

Note the function as defined just multiplies the input by 3, and prints the result at each multiple.

请注意,定义的函数只是将输入乘以 3,并在每个倍数处打印结果。