Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bunny Ears 2
- We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears.
- The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2, ... n (without loops or multiplication).
- Input
- On the first line you will be given the number of bunnies.
- Output
- On the only output line you should print the number of their ears.
- Sample tests
- Input
- 1
- Output
- 2
- Input
- 3
- Output
- 7
- import java.util.Scanner;
- public class BunnyEars2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- System.out.println(ears2(n));
- }
- public static int ears2(int n){
- if(n == 0){
- return 0;
- }
- if(n % 2 == 0) return 3 + ears2(n-1);
- else return 2 + ears2(n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment