24 game problem

This is a discussion on 24 game problem within the C++ Programming forums, part of the General Programming Boards category; Okay, this isn't really a game.
I had a math problem, and i wanted to make a program where you ...

24 game problem

I had a math problem, and i wanted to make a program where you enter 4 numbers, and the program finds what combinations can be used without exponents or grouping signs and would equal 24.

I have the adding/subtractions part working, just a few small problems with grouping, but i dont want to use if else if the whole time. I addded the mulitplication and division, just to have it break out of the loop.

Is there an algorithim i could use for this, or a certain command? Thanks.

// edit although now that I think about it, for multiplication and division you'll only do that operation with the previous number, not the whole current total. I'd suggest doing similar to above, but instead adding the operator and the number to a string instead of add/subtract/etc with an int. Then when you hit the max recursion depth, you have a function parse the string to figure out what the total is.

Vectors are nothing more than smarter arrays really. Just substitute in an array and it would work the same. The benefit of using recursion this way is that it will test all possibilities, not just some.

Okay, I'll show you a simple example which you'd have to update to accomodate the bigger numbers for your program. Say you have a function called

Code:

solve(string s)

which takes a string like "1+3*4-9" and is able to parse and figure out what that is equal to, and if you want maybe print out the equation if it equals a certain number. Then your recursion would look something like this (i haven't tested it though!):

There are ways to optimize this better, just a quick idea. Its basically equivalent to four nested for loops running through all of the operator possibilities in between each pair of numbers and solving it in the innermost for loop. The reason I chose recursion is that it can easily be done to say ten numbers instead of only four without having to change anything.

In pseudo-code, it looks something like this
// prototype
bool solve ( int target, int values[], int numvals );
// The initial call would be
int nums[] = { 1, 2, 3, 4 };
bool result = solve( 24, nums, 4 );
The basic idea is that if you have
1 + (solve("2,3,4")) == 24
You can rearrange that into
solve(24-1,"2,3,4")
That is to say, knowing that adding 1 to the result will
make 24, you can solve the recursive step for 24-1
Similarly, if you had
2 * (solve("1,3,4") == 24
You would invoke
solve(24/2,"1,3,4")
At each recursive level you try
- 4 operators
- each element of the array
Eventually, you end up with a simple case where the function is called
with a single number and a single target value. If they match, you
have an answer to the problem.

Just check out my best work at http://blizzarddog.tk and get TDOD and TDOD Music Pack from the projects->released page:
Thats my best... And yes, based on if else if statements, a bunch of switch case statements and voids.