简体   繁体   中英

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?

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.

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:

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

So, we know n is always supposed to be 0 or greater. We also know that the method is supposed to return when n is equal to 0, as evidenced by:

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.

We see two different possible scenarios to take n 's greater than zero and do something with them:

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.

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.

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. Since 2 is an even number (2 % 2 is 0), the second return is used. This means that 3 + bunnyEars(1) is called.

Since 1 is also greater than 0, we go to the recursive methods again. Since 1 is an odd number (1 % 2 is 1), the first recursive return is called. This means we call 2 + bunnyEars(0).

Now, since n is 0, the return n from the if-statement is used:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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