-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path679.cpp
More file actions
35 lines (29 loc) · 968 Bytes
/
679.cpp
File metadata and controls
35 lines (29 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
bool judgePoint24(vector<int>& nums) {
vector<double> a(nums.begin(), nums.end());
return solve(a);
}
bool solve(vector<double> a) {
const double eps = 0.001;
if(a.size() == 1) return abs(a[0] - 24) < eps;
for (int i=0; i<a.size(); i++) {
for (int j=i+1; j<a.size(); j++) {
vector<double> b(a.size() -1);
for (int k=0, index=0; k<a.size(); k++) {
if(k!=i && k!=j) {
b[index++] = a[k];
}
}
for (double d: compute(a[i], a[j])) {
b[b.size()-1] = d;
if(solve(b)) return true;
}
}
}
return false;
}
vector<double> compute(double x, double y) {
return {x+y, x-y, y-x, x*y, x/y, y/x};
}
};