class Solution {
public:
const double eps = 1e-6;
bool judgePoint24(vector<int>& nums) {
vector<double>l;
for(int i=0;i<nums.size();i++){
l.push_back(nums[i]);
}
return solve(l);
}
bool solve(vector<double> &l){
if(l.size() == 0)
return false;
if(l.size() == 1){
return (fabs(l[0] - 24) <= eps);
}
int size = l.size();
for(int i=0;i<size;i++){//第i个和第j个元素运算
for(int j=0;j<size;j++){
if(i == j)
continue;
vector<double>temp;
for(int k=0;k<size;k++){
if(k!=i&&k!=j)
temp.push_back(l[k]);//其他元素不影响
}
//+,-,*,/
for(int k=1;k<=4;k++){
if(k == 1){
temp.push_back(l[i] + l[j]);
}
if(k == 2){
temp.push_back(l[i] - l[j]);
}
if(k == 3){
temp.push_back(l[i] * l[j]);
}
if(k == 4){
if(fabs(l[j]) <= eps){
continue;
}
temp.push_back(l[i]/l[j]);
}
if(solve(temp))
return true;
temp.pop_back();
}
}
}
return false;
}
};