# Solution for We Count to 24
https://acm.cs.nthu.edu.tw/problem/14072/
The solution with this question starts with the number of numbers after every iteration. Once an operation is used, the total number available decreases by 1 hence by the third iteration, there will be only one number left, this will be our base case.
The next question is how do we make it so that the code will pass on the numbers left to the next iteration. Hard coding the inputs will cause more trouble than its worth, especially if the inputs were to change or be infinite. By creating a new array at every iteration to store the new values to be passed, the recursive function becomes more consistent.
One can use malloc to create a specific sized array, or they can use length variable to keep track of how many numbers are left.
All thats left after that is to fill in the array by selecting every combination of 2 numbers and storing whatever isnt used to the array. Once an operation is used on the 2 numbers, it will be stored to the array as well and then used in the next recursive call.
```cpp=
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#define EPSILON 1e-6
bool judgePoint24(double cards[], int size)
{
if (size == 1) {
return fabs(cards[0] - 24.0) < EPSILON;
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i != j)
{
double newCards[4];
int newSize = 0;
for (int k = 0; k < size; k++)
{
if (k != i && k != j) {
newCards[newSize++] = cards[k];
}
}
newCards[newSize] = cards[i] + cards[j];
if (judgePoint24(newCards, newSize + 1)) return true;
newCards[newSize] = cards[i] - cards[j];
if (judgePoint24(newCards, newSize + 1)) return true;
newCards[newSize] = cards[i] * cards[j];
if (judgePoint24(newCards, newSize + 1)) return true;
if (cards[j] != 0)
{
newCards[newSize] = cards[i] / cards[j];
if (judgePoint24(newCards, newSize + 1)) return true;
}
}
}
}
return false;
}
int main()
{
double cards[4];
int q;
scanf("%d", &q);
while(q --)
{
for(int i = 0; i < 4; i ++)
{
scanf("%lf", &cards[i]);
}
bool result = judgePoint24(cards, 4);
if (result) {
printf("Safe\n");
}
else {
printf("Disqualified\n");
}
}
return 0;
}
```