简体   繁体   English

有人可以向我解释这个递归背后的逻辑吗

[英]Can someone explain to me the logic behind this recursion

public int bunnyEars(int n) {
    if (n < 0) {
        throw new IllegalArgumentException();
    }
    if (n == 0) {
        return n;
    }
    if (n % 2 == 1)
       return 2 + bunnyEars(n - 1);

   return 3 + bunnyEars(n - 1);
}

can someone explain how bunnyEars(2) = 5 and also how it works?有人能解释一下bunnyEars(2) = 5如何工作的吗?

From your comments, I understand that you already know the meaning of a recursive call.从您的评论中,我了解到您已经知道递归调用的含义。 In order to make it clear how it works, you can trace the calls in some way.为了弄清楚它是如何工作的,您可以通过某种方式跟踪调用。 Given below is an example of one of the ways:下面给出了其中一种方法的示例:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println(bunnyEars(2));
    }

    static int bunnyEars(int n) {
        if (n < 0) {
            throw new IllegalArgumentException();
        }
        if (n == 0) {
            System.out.println(n);
            return n;
        }
        if (n % 2 == 1) {
            System.out.print("2 + bunnyEars(" + n + "-1) -> ");
            return 2 + bunnyEars(n - 1);
        }
        System.out.print("3 + bunnyEars(" + n + "-1) -> ");
        return 3 + bunnyEars(n - 1);
    }
}

Output:输出:

3 + bunnyEars(2-1) -> 2 + bunnyEars(1-1) -> 0
5

As you can see, 3 + 2 + 0 = 5 is what you get as the answer.如您所见,3 + 2 + 0 = 5 是您得到的答案。

I hope, it helps.我希望,它有帮助。 Feel free to comment in case of any doubt.如有任何疑问,请随时发表评论。

If the number n is less than 0, then an IllegalArgumentException is thrown, as evidenced by:如果数字n小于 0,则抛出 IllegalArgumentException,如下所示:

if (n < 0) {
   throw new IllegalArgumentException();
}

So, we know n is always supposed to be 0 or greater.所以,我们知道n总是应该是 0 或更大。 We also know that the method is supposed to return when n is equal to 0, as evidenced by:我们也知道该方法应该在n等于 0 时返回,如下所示:

if (n == 0) {
    return n;
 }

So, presumably, the method public int bunnyEars(int n) will take a number equal to or greater than zero, and will start adding integers until the n is zero.因此,可以推测, public int bunnyEars(int n)将取一个等于或大于零的数字,并将开始添加整数,直到n为零。

We see two different possible scenarios to take n 's greater than zero and do something with them:我们看到两种不同的可能场景,将n大于零并对其进行处理:

if (n % 2 ==1)
   return 2 + bunnyEars(n-1);
return 3 + bunnyEars(n -1); //else

What is happening here is if the n % 2 is equal to one (meaning that the number is odd, since every odd integer divided by two has a remainder of one), then the method is recursively called with the current n minus 1, and the final integer to return is incremented by two.这里发生的情况是,如果n % 2 等于 1(意味着该数为奇数,因为每个奇数除以 2 的余数为 1),则使用当前n减 1 递归调用该方法,并且要返回的最终整数加 2。

If the number is not odd (and is thus even), then the same thing is happening, but with the final integer to return incremented by 3.如果数字不是奇数(因此是偶数),则发生同样的事情,但最终返回的整数增加了 3。

So in your example of bunnyEars(2) , we see that the n of 2 is both positive and greater than zero, so no error is thrown and the method does not return without recursion.因此,在您的bunnyEars(2)示例中,我们看到 2 的 n 既为正又大于零,因此不会引发错误并且该方法不会在没有递归的情况下返回。 Since 2 is an even number (2 % 2 is 0), the second return is used.由于 2 是偶数(2 % 2 为 0),因此使用第二个return值。 This means that 3 + bunnyEars(1) is called.这意味着 3 + bunnyEars(1) 被调用。

Since 1 is also greater than 0, we go to the recursive methods again.由于 1 也大于 0,我们再次转到递归方法。 Since 1 is an odd number (1 % 2 is 1), the first recursive return is called.由于 1 是奇数(1 % 2 是 1),因此调用第一个递归返回。 This means we call 2 + bunnyEars(0).这意味着我们调用 2 + bunnyEars(0)。

Now, since n is 0, the return n from the if-statement is used:现在,因为 n 是 0,所以使用 if 语句的return n

if (n == 0) {
    return n;
 }

On the first round we had the final integer to return incremented by 3, and on the second time it was incremented by 2, for a total of 5. So the result is five.在第一轮我们返回的最后一个整数增加了 3,第二次它增加了 2,总共是 5。所以结果是 5。

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

相关问题 我不明白这段代码背后的逻辑。 有人可以向我解释它的真正作用和方式吗? - i do not get the logic behind this code. Can someone please explain to me what it really does and how? 有人可以向我解释这段代码最后一部分的逻辑吗? - Can someone explain to me the logic of the last part of this code? 有人可以解释此代码中的递归 - Can someone explain the Recursion in this Code 有人可以向我解释在Java中传递“值”而不是“引用”背后的原因是什么? - Can someone explain to me what the reasoning behind passing by “value” and not by “reference” in Java is? 简单的 java 递归,有人可以帮我理解堆栈 memory 中发生的逻辑吗? - Simple java recursion, Can someone help me understand the logic going on in the stack memory? 有人可以向我解释这个功能是如何工作的吗? (递归) - Could someone explain to me how this function works? (recursion) 有人可以向我解释这种递归方法吗? - Can someone explain me this recursive method? 有人可以向我解释此nullPointerException错误的详细信息吗? - Can someone explain the details of this nullPointerException error to me? 有人可以解释一下这个小代码吗? - Can someone explain me this little code? 有人可以向我解释基本的 Java Generics 吗? - Can someone explain basic Java Generics to me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM