Here's my ideas on how to solve it
Code:
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.