简体   繁体   English

递归方法的结果

[英]Result of recursive method

I am trying to understand this recursive method but even with a debugger I couldn't come up to something that makes sense to me so i hope someone here is motivated to explain me what is going on here. 我试图理解这种递归方法,但是即使使用调试器,我也无法解决对我来说有意义的事情,因此我希望这里的人有动力向我解释这里的情况。 I know how recursion works basically but the way this method is written down troubles me. 我知道递归的工作原理,但是写下这种方法的方式困扰着我。 I know the conditional operator in java and I worked out a new code but still I don't understand it, so what I expect here is. 我知道Java中的条件运算符,我编写了一个新代码,但是仍然不明白,所以我期望这里是。

What is the result for m(5) AND m(15). m(5)和m(15)的结果是什么。 How did you calculate it? 您是如何计算的? Thanks 谢谢

Edit after the answers. 答案后编辑。 I made a table with my results for future readers 我整理了一张表格,供以后的读者参考

m(n)::|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|...
result|0|0|1|1|1|2|3|4|6|9|13|19|28|41|60|88|...

I checked only the result of 15 with my program. 我只用程序检查了15的结果。

public class practice {

    /**
     * Try to understand the way this works
     * @param n
     * @return 
     */
    static long m(long n) {
        return n <= 1 ? 0 : n == 2 ? 1 : m(n - 1) + m(n - 3);
    }

    /**
     * This is what i tried so far.
     * @param n
     * @return 
     */
    static long ma(long n) {

        System.out.println("Method called");
        System.out.println("N is: " + n);
        if (n <= 1) {
            System.out.println("N<=1: " + n);
            System.out.println("0 is returned");
            return 0;
        } else if (n == 2) {
            System.out.println("N==2: " + n);
            System.out.println("1 is returned");
            return 1;
        } else {
            System.out.println("Inside ELSE, N is: " + n);
            return ma(n - 1) + ma(n - 3);
        }
    }

    public static void main(String[] args) {
        ma(15);
    }

}

Wrote it this way to make it more understandable: 用这种方式编写它,使它更易于理解:

m(0) = 0
m(1) = 0
m(2) = 1
m(n) = m(n - 1) + m(n - 3) // n >= 3

When we know the values for m(0) , m(1) and m(2) , we can calculate any m(n) , where n >= 3 , using m(n - 1) + m(n - 3) . 当我们知道m(0)m(1)m(2) ,我们可以使用m(n - 1) + m(n - 3)计算任何m(n) ,其中n >= 3 For any negative input, the result is 0. 对于任何负输入,结果均为0。

For example: 例如:

m(3) = m(3 - 1) + m(3 - 3) = m(2) + m(0) = 1 + 0 = 1
m(4) = m(4 - 1) + m(4 - 3) = m(3) + m(1) = 1 + 0 = 1
m(5) = m(5 - 1) + m(5 - 3) = m(4) + m(2) = 1 + 1 = 2

And so on... 等等...

Pencil and paper are you friends. 铅笔和纸是您的朋友。

There are 3 cases (two of them are base conditions) 有3种情况(其中两种是基本情况)

if n <= 1  then return 0

if n == 2 then return 1

else recursive call to  m(n-1) + m(n-3)

So you know that on every recursive call we are approaching one of the base conditions. 因此,您知道在每个递归调用中,我们都接近基本条件之一。

Here is a stack trace of what happens on m(5) 这是m(5)发生的堆栈跟踪

                                m(5)
                    m(4)         +          m(2)
        m(3)         +      m(1)            return 1
m(2)     +      m(0)        return 0
return 1        return 0

Adding all the returns gives 1 + 0 + 0 + 1 which is 2 将所有收益相加得出1 + 0 + 0 + 1 ,即2

So 所以

m(5) == 2

Method m in pseudo code. 伪代码中的方法m (this is some kind of scala/python mash up) (这是某种scala / python混搭)

def m (number n)
    if (n <= 1)       0
    else if (n == 2)  1
    else              m(n - 1) + m(n - 3)

Looking at this you can see that anything <= 2 is a terminal operation, returning 0 or 1 based on the input. 查看此内容,您可以看到<= 2任何内容都是终端操作,根据输入返回01 If n is > 2, the fun begins. 如果n > 2,则乐趣开始。 Take for example n=3 . n=3为例。 Since 3 is greater than 2 , we need to run the third if . 由于3大于2 ,因此我们需要运行第三个if When we look at that line we see that we nee to return m(n - 1) + m(n - 3) . 当我们看那条线时,我们看到nee返回m(n - 1) + m(n - 3) Plugging n = 3 in, it looks like this: m(3 - 1) + m(3 - 3) , or, more simplified, like this: m(2) + m(0) . 插入n = 3后,它看起来像是: m(3 - 1) + m(3 - 3) ,或更简单地说,是这样的: m(2) + m(0) This will now terminate with 1 because neither 2 or 0 for n will result in more calling of m . 现在这将以1终止,因为n 20都不会导致m更多调用。

So now we have something we understand we can now workout what m(15) and m(5) will return. 现在我们有了一些了解,现在我们可以锻炼m(15)m(5)会返回什么。

I will only work it out for 5 since the call stack for 15 would be way to long 我只会计算5因为15的调用栈会很长

                   m(5)
              /           \
           m(5-1)   +    m(5-3)
        /         \         |
      m(4-1)  +  m(4-3)     |
    /       \       |       |
  m(3-1) + m(3-3)   |       |
    |        |      |       |
    1    +   0   +  0   +   1
                 2

Hope it helps! 希望能帮助到你!

m(5) = m(4)+m(2) // 5>2, so the third condition
m(4) + m(2) = m(3)+m(1) + 1 // 4>2, so the third condition
m(3) + m(1) + 1 = m(2)+m(0) + 0 + 1 // 3>2, so the third condition
m(2) + m(0) + 0 + 1 = 1 + 0 + 0 + 1 = 2

Now, between transformations, m(2) s instantly replaced with 1 , and m(n<=1) is instantly replaced with 0 . 现在,在转换之间, m(2)立即替换为1 ,而m(n<=1)立即替换为0 This is how you can analyze this on paper. 这就是您可以在纸上进行分析的方式。 Computer, however, would wirst calculate m(4) before calculating m(2) and adding the results in the first line - this happens because of order of poerations within the recursive functions. 但是,计算机在计算m(2)并将结果加到第一行之前会先计算m(4) -发生这种情况是由于递归函数中的插补顺序。

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

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