sunfresh

BunnyEars2

Jan 20th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Bunny Ears 2
  2. We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears.
  3. 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).
  4.  
  5. Input
  6. On the first line you will be given the number of bunnies.
  7.  
  8. Output
  9. On the only output line you should print the number of their ears.
  10.  
  11. Sample tests
  12. Input
  13. 1
  14. Output
  15. 2
  16. Input
  17. 3
  18. Output
  19. 7
  20.  
  21. import java.util.Scanner;
  22.  
  23. public class BunnyEars2 {
  24. public static void main(String[] args) {
  25. Scanner scanner = new Scanner(System.in);
  26. int n = Integer.parseInt(scanner.nextLine());
  27. System.out.println(ears2(n));
  28.  
  29. }
  30. public static int ears2(int n){
  31. if(n == 0){
  32. return 0;
  33. }
  34. if(n % 2 == 0) return 3 + ears2(n-1);
  35. else return 2 + ears2(n-1);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment