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