Recursion JAVA I will be testing through eclipse Question: A Teddy Bear Picnic T
ID: 669985 • Letter: R
Question
Recursion JAVA I will be testing through eclipse
Question: A Teddy Bear Picnic
This question involves a game with teddy bears. The game starts when I give
you some bears. You can then give back some bears, but you must follow these
rules (where n is the number of bears that you have):
1. If n is even, then you may give back exactly n/2 bears.
2. If n is divisible by 3 or 4, then you may multiply the last two digits of n
and give back this many bears. (By the way, the last digit of n is n%10,
and the next-to-last digit is ((n%100)/10).
3. If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.
For example, suppose that you start with 250 bears. Then you could make these
moves:
• --Start with 250 bears.
• --Since 250 is divisible by 5, you may return 42 of the bears, leaving you
with 208 bears.
• --Since 208 is even, you may return half of the bears, leaving you with
104 bears.
• --Since 104 is even, you may return half of the bears, leaving you with
52 bears.
• --Since 52 is divisible by 4, you may multiply the last two digits
(resulting in 10) and return these 10 bears. This leaves you with 42
bears.
• --You have reached the goal!
Write a recursive method to meet this specification:
public static boolean bears(int n)
// Postcondition: A true return value means that it is possible
// to win the bear game by starting with n bears. A false return
// value means that
// it is not possible to win the bear game by starting with n
// bears.
// Examples:
// bear(250) is true (as shown above)
// bear(42) is true
// bear(84) is true
// bear(53) is false
// bear(41) is false
Hint: To test whether n is even, use the expression ((n % 2) == 0).
Explanation / Answer
As per your discussion, there is a minute flaw in the flow.
Start with 250, as it is divisible by 5, you will return 42 bears and will be left with 208 bears.
At this point, there are two possibilities. 208 is divisible by both 4 and also by 2. So, I presumed it to be, even if the number is divisible by either 3 or 4, if the multiplication of the last 2 digits is 0(which will lead to indefinite recursion), I suggested to move on to the next level, by checking with 2.
With these considerations, the code is given below.....
import java.util.Scanner;
public class TeddyBears
{
public static boolean bears(int num)
{
if(num < 42)
return false;
else if(num == 42)
return true;
else if(num % 5 == 0)
return bears(num - 42);
else if((num % 3 == 0 || num % 4 == 0) && (((num%10) * ((num%100)/10)) != 0))
return bears(num - ((num%10) * ((num%100)/10)));
else if(num % 2 == 0)
return bears(num / 2);
return false;
}
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int num = in.nextInt();
boolean flag = bears(num);
if(flag)
System.out.println("You are left with 42 bears. You have reached the goal.");
else
System.out.println("You can't reach your goal, if you start with "+num+" bears.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.