recursive function for the bear game

C

ChuckB

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.
 
D

Daniel T.

ChuckB said:
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?" );
 
D

Daniel T.

"ChuckB said:
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);"
 
M

Mark P

ChuckB said:
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?
 
C

ChuckB

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

Daniel T.

ChuckB said:
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:
# --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 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top