-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24Game679.cpp
More file actions
32 lines (31 loc) · 1006 Bytes
/
24Game679.cpp
File metadata and controls
32 lines (31 loc) · 1006 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
class Solution {
public:
vector<double> compute(double a, double b) {
vector<double>ans = {a+b, a-b, b-a, a*b, a/b, b/a};
return ans;
}
bool go(vector<double>& num) {
if (num.size() == 1) {
if (abs(num[0]-24.0) < 0.001) return true;
return false;
}
for (int i = 0; i < num.size(); i++) {
for (int j = i+1; j < num.size(); j++) {
for (double c : compute(num[i], num[j])) {
vector<double>next;
next.push_back(c);
for (int k = 0; k < num.size(); k++) {
if (k == j || k == i) continue;
next.push_back(num[k]);
}
if (go(next)) return true;
}
}
}
return false;
}
bool judgePoint24(vector<int>& cards) {
vector<double>num(cards.begin(), cards.end());
return go(num);
}
};