recursive function for the bear game

Discussion in 'C++' started by ChuckB, Nov 8, 2006.

1. ChuckBGuest

Ok, heres the code i've come up with

#include <iostream>
using namespace std;
bool bear (int);

void main( )
{
bear(250); // is true
bear(42); // is true
bear(84); // is true
bear(53); // is false
bear(41); // is false
}

bool bear(int n)
{
cout << n;
if (n == 42)
{
cout << "true" << endl;
return true;
}
else
{
if (n % 2 == 0)
{
n = n/2;
bear (n);
}
else if ((n % 3 == 0)||(n % 4 == 0))
{
if ((n % 10 != 0) || (((n%100)/10) != 0))
{
int t = ((n%100)/10)*(n % 10);
n = n - t;
bear(n);
}
}
else if (n % 5 == 0)
{
n = n - 42;
bear(n);
}
else
{
cout << "false" << endl;
return false;
}
}
}

The problem is of course that bear(250); will not equal true because of
the order of the commands. I guess i'm going to need to do backtracking
on this, however I am very confused when i comes to doing backtracking.
If anyone could help me it would be greatly apprietiated.

ChuckB, Nov 8, 2006

2. Daniel T.Guest

"ChuckB" <> wrote:

> Ok, heres the code i've come up with

I'm not sure what "the bear game" is, but if you add the line I indicate
below, you will see where your problem lies.
>
> #include <iostream>
> using namespace std;
> bool bear (int);
>
> void main( )
> {
> bear(250); // is true
> bear(42); // is true
> bear(84); // is true
> bear(53); // is false
> bear(41); // is false
> }
>
> bool bear(int n)
> {
> cout << n;
> if (n == 42)
> {
> cout << "true" << endl;
> return true;
> }
> else
> {
> if (n % 2 == 0)
> {
> n = n/2;
> bear (n);
> }
> else if ((n % 3 == 0)||(n % 4 == 0))
> {
> if ((n % 10 != 0) || (((n%100)/10) != 0))
> {
> int t = ((n%100)/10)*(n % 10);
> n = n - t;
> bear(n);
> }
> }
> else if (n % 5 == 0)
> {
> n = n - 42;
> bear(n);
> }
> else
> {
> cout << "false" << endl;
> return false;
> }
> }

assert( false && "what should happen here?" );
> }
>
> The problem is of course that bear(250); will not equal true because of
> the order of the commands. I guess i'm going to need to do backtracking
> on this, however I am very confused when i comes to doing backtracking.
> If anyone could help me it would be greatly apprietiated.

--
To send me email, put "sheltie" in the subject.

Daniel T., Nov 8, 2006

3. Daniel T.Guest

In article <>,
"ChuckB" <> wrote:

> Ok, heres the code i've come up with
>
> #include <iostream>
> using namespace std;
> bool bear (int);
>
> void main( )
> {
> bear(250); // is true
> bear(42); // is true
> bear(84); // is true
> bear(53); // is false
> bear(41); // is false
> }
>

Everywhere in the function below where you call "bear(n);" you need to
change it to "return bear(n);"

> bool bear(int n)
> {
> cout << n;
> if (n == 42)
> {
> cout << "true" << endl;
> return true;
> }
> else
> {
> if (n % 2 == 0)
> {
> n = n/2;
> bear (n);
> }
> else if ((n % 3 == 0)||(n % 4 == 0))
> {
> if ((n % 10 != 0) || (((n%100)/10) != 0))
> {
> int t = ((n%100)/10)*(n % 10);
> n = n - t;
> bear(n);
> }
> }
> else if (n % 5 == 0)
> {
> n = n - 42;
> bear(n);
> }
> else
> {
> cout << "false" << endl;
> return false;
> }
> }
> }
>
> The problem is of course that bear(250); will not equal true because of
> the order of the commands. I guess i'm going to need to do backtracking
> on this, however I am very confused when i comes to doing backtracking.
> If anyone could help me it would be greatly apprietiated.

--
To send me email, put "sheltie" in the subject.

Daniel T., Nov 8, 2006
4. Mark PGuest

ChuckB wrote:
> Ok, heres the code i've come up with
>

[snip]
>
> The problem is of course that bear(250); will not equal true because of
> the order of the commands. I guess i'm going to need to do backtracking
> on this, however I am very confused when i comes to doing backtracking.
> If anyone could help me it would be greatly apprietiated.
>

How can we possibly help you when you haven't explained to any of us
what you're trying to do?

Mark P, Nov 8, 2006
5. ChuckBGuest

I guess it would help to explain the problem. Sorry about that... Here
it is.

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:
# --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 function to meet this specification:

ChuckB, Nov 8, 2006
6. Daniel T.Guest

"ChuckB" <> wrote:

> I guess it would help to explain the problem. Sorry about that... Here
> it is.
>
> 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:
> # --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 function to meet this specification:

Your going to have to write a search routine to solve this one, probably
a depth first search. Read up on them. The code you currently have is
unsuitable.

--
To send me email, put "sheltie" in the subject.

Daniel T., Nov 8, 2006