简体   繁体   English

此递归函数如何到达此输出?

[英]How does this recursive function get to this output?

When I run this piece of code for n=5 the output I get is "5 3 1 1 3 5" I get the 5 3 1 part but after that n=-1 but when I run the code with a debugger it when n=-1 it goes to the line after numbers(n-2); 当我为n = 5运行这段代码时,我得到的输出是“ 5 3 1 1 3 5”,我得到了5 3 1部分,但是在那之后n = -1,但是当我使用调试器运行代码时,当n = -1转到numbers(n-2);之后的行numbers(n-2); ie System.out.prt(n+ ""); System.out.prt(n+ ""); even though that statement is contained in the if block. 即使该语句包含在if块中。

Why does this happen? 为什么会这样?

public void numbers(int n)
{
    if(n>0)
    {
        System.out.print(n+" ");
        numbers(n-2);
        System.out.print(n+" ");
    }
}

TLDR : when n=-1 System.out.prt(n+ ""); TLDR:当n = -1时System.out.prt(n+ ""); even though it is within the if block which only runs when n>0. 即使它位于if块中,该块仅在n> 0时运行。

Any help would be much appreciated. 任何帮助将非常感激。 Thanks in advance! 提前致谢!

Remove the final System.out.print(n+" "); 删除最终的System.out.print(n +“”); After the recursive numbers call it comes back with the original value for n and prints this again. 递归数字调用后,它返回n的原始值并再次打印。

It bubbles back from the deepest level where it prints 1 two times up to the call with number 3, which is printed again, and also 5 is printed again. 它从最深的级别冒泡回去,在最深的级别上它两次打印1次,直到再次打出编号3的呼叫,再打出5个。

If you want it to update the value after it is printed the first time you will have to update the variable n by performing n-=2 instead of n-2 如果希望它在第一次打印后更新值,则必须通过执行n- = 2而不是n-2来更新变量n

Here is what happens behind the scenes, for n == 5 : 对于n == 5 ,这是幕后发生的事情:

numbers(5);
    if(5 > 0)--> true : 
        System.out.print(5 + " "); // (1)
        numbers(3);
        |   if(3 > 0)--> true : 
        |       System.out.print(3 + " "); // (2)
        |       numbers(1);
        |       |   if(1 > 0)--> true : 
        |       |       System.out.print(1 + " "); // (3)
        |       |       numbers(-1);
        |       |       System.out.print(1 + " "); // (4)
        |       System.out.print(3 + " "); // (5)
        System.out.print(5 + " "); // (6)

Notice how each number is supposed to be printed twice: 注意每个数字应该如何打印两次:

System.out.print(n + " "); // print once
numbers(n-2);
System.out.print(n + " "); // print twice
    5 first system.out then number(5-2)
    |
    ----> 3  first system.out then number(5-2)
          |
           ----->1 first system.out then number(5-2)
                 (smaller than 0) , returning
                 |
                 1 second system.out
                 |
           3<----  second system.out
           |
5<---------  second system.out

after n=-1, the recursive call will end and return back to the caller method to continue. 在n = -1之后,递归调用将结束并返回到调用方方法以继续。 first commands it found is print. 找到的第一个命令是打印。

If you try to debug with the debugger, you will see how rational it is. 如果尝试使用调试器进行调试,您将看到它的合理性。

Java将方法调用维护到堆栈中,并在输出第一个5,3,1之后打印该方法调用,然后继续执行其余调用,并从堆栈中打印1,3,5(首先拾取堆栈中的最后一个调用)。

Follow the recursion stack. 遵循递归堆栈。

stack representation with and output flow: 具有输出流的堆栈表示:

             ------------------------->
             5           3            1
num(5)-->  num(3) --> num(1) --> num(-1) --> void
             5           3            1
             <-------------------------

Now since numbers(-1) does not satisfy the if condition, the program control comes out of the if block, and returns void. 现在,由于numbers(-1)不满足if条件,因此程序控件来自if块,并返回void。 Start popping out the stack now (recursion): 5 3 1 1 3 5. That's what the output you get, and is expected. 现在开始弹出堆栈(递归):5 3 1 1 3 5.这就是您得到的输出,并且可以预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM