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

发布于 2024-10-29 16:17:11 字数 736 浏览 1 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

非要怀念 2024-11-05 16:17:13
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
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
山色无中 2024-11-05 16:17:13

您对三元组(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

千仐 2024-11-05 16:17:12

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

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

现在假设 triple(0)triple(1) 等都是单独的变量,并求解 triple(5)< /code> 通过努力向上。

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.

<逆流佳人身旁 2024-11-05 16:17:12

那么它不会先减到零(减去 1),然后为每个加 3(0 3 6 等)。

这是我得到的输出:

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

它所做的是从每个枚举中的 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:

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

橘虞初梦 2024-11-05 16:17:12

您的输出应如下所示:

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)

这是因为 Triple(n) 会打印消息之前调用 Triple(n-1)。所以你的三重(5)消息将最后打印。

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) before printing out the message. So your triple(5) message will be printed last.

淡莣 2024-11-05 16:17:12

当执行到达该点时,三元组方法从头开始再次执行。一旦返回,执行将在下一行继续。这递归地发生

所以执行顺序是这样的:

  1. if (n == 0) // n == 5 此时,条件为 false
  2. Total = 3 + Triple(n-1) // 我们必须计算 Triple(4)
  3. if ( n == 0) // 现在 n == 4。
  4. Total = 3 + Triple(n-1) // 我们必须计算 Triple(3)
  5. ... 等等,直到 n == 0:
  6. if (n == 0) // true!
  7. return 0 // 返回 0
  8. Total = 3 + 0 // 0 来自 Triple(n-1),它刚刚返回 0
  9. System.out.println(total); // 打印 3
  10. 返回总计; // 返回 3
  11. 总计 = 3 + 3; // 同样,3 来自 Triple(n-1),其中 n==2
  12. System.out.println(total); // 这次打印 6
  13. ...等等。

请注意,定义的函数只是将输入乘以 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:

  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.

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

撩发小公举 2024-11-05 16:17:12
public static int triple(int n)
{

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

忽略 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)

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

再次感谢!

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.

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.

THANKS AGAIN!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文