-
Notifications
You must be signed in to change notification settings - Fork 22
/
0067.cpp
62 lines (58 loc) · 1.5 KB
/
0067.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
57
58
59
60
61
62
// 0067.24点游戏算法
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
double op(double a, double b, int opera)
{
if(opera == 0) return a+b;
else if(opera == 1) return a-b;
else if(opera == 2) return a*b;
else if(opera == 3) return a/b;
return 0;
}
bool cal24(vector<double> a, vector<int> o)
{
// 考虑括号导致优先级变化
bool flag = false;
if(o.empty()) {
if( fabs(a[0]-24.0) < 0.01) flag = true;
} else {
for (int i = 0; i < o.size() && !flag; i++)
{
a[i] = op(a[i], a[i+1], o[i]);
a.erase(a.begin()+i+1);
o.erase(o.begin()+i);
flag |= cal24(a, o);
}
}
return flag;
}
int main()
{
double a[4];
int o[3];
while(cin >> a[0] >> a[1] >> a[2] >> a[3])
{
bool flag = false;
sort(a, a+4);
do {
for(int i = 0; i < 4 && !flag; i++) {
o[0] = i;
for(int j = 0; j < 4 && !flag; j++) {
o[1] = j;
for(int k = 0; k < 4 && !flag; k++) {
o[2] = k;
vector<double> va(a, a+4);
vector<int> vo(o, o+3);
if(cal24(va, vo)) flag = true;
}
}
}
} while (next_permutation(a, a+4) && !flag);
if( flag ) cout << "true" << endl;
else cout << "false" << endl;
}
return 0;
}