-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path24_Game.cpp
56 lines (48 loc) · 1.67 KB
/
24_Game.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
const double EPS = 0.0001;
bool isPossible(vector<int>& nums) {
double a = nums[0], b = nums[1], c = nums[2], d = nums[3];
if(isPossible(a + b, c, d)
or isPossible(a - b, c, d)
or isPossible(a * b, c, d)
or (b and isPossible(a / b, c, d))) return true;
if(isPossible(a, b + c, d)
or isPossible(a, b - c, d)
or isPossible(a, b * c, d)
or (c and isPossible(a, b / c, d))) return true;
if(isPossible(a, b, c + d)
or isPossible(a, b, c - d)
or isPossible(a, b, c * d)
or (d and isPossible(a, b, c / d))) return true;
return false;
}
bool isPossible(double a, double b, double c) {
if(isPossible(a + b, c)
or isPossible(a - b, c)
or isPossible(a * b, c)
or (b and isPossible(a / b, c))) return true;
if(isPossible(a, b + c)
or isPossible(a, b - c)
or isPossible(a, b * c)
or (c and isPossible(a, b / c))) return true;
return false;
}
bool isPossible(double a, double b) {
if (abs(a + b - 24.0) < EPS
or abs(a - b - 24.0) < EPS
or abs(a * b - 24.0) < EPS
or (b and abs(a / b - 24.0) < EPS))
return true;
return false;
}
public:
bool judgePoint24(vector<int>& nums) {
sort(nums.begin(), nums.end());
do{
if(isPossible(nums)) {
return true;
}
} while(next_permutation(nums.begin(), nums.end()));
return false;
}
};