By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

recursive function for the bear game

P: n/a
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.

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"ChuckB" <CE*********@gmail.comwrote:
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.
Nov 8 '06 #2

P: n/a
In article <11**********************@h54g2000cwb.googlegroups .com>,
"ChuckB" <CE*********@gmail.comwrote:
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.
Nov 8 '06 #3

P: n/a
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?
Nov 8 '06 #4

P: n/a
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:

Nov 8 '06 #5

P: n/a
"ChuckB" <CE*********@gmail.comwrote:
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.

--
To send me email, put "sheltie" in the subject.
Nov 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.