-
Notifications
You must be signed in to change notification settings - Fork 0
/
24game.cpp
50 lines (48 loc) · 1.46 KB
/
24game.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
bool judgePoint24(vector<int>& nums)
{
map<int,set<float>> list;
for(int i=0;i<4;++i)
{
int mask=1<<i;
for(int j=0;j<4;++j)
{
if(i==j)
continue;
int t=mask|1<<j;
set<float> temp;
temp.insert(nums[i]+nums[j]);
temp.insert(nums[i]*nums[j]);
temp.insert(nums[i]-nums[j]);
temp.insert(nums[j]-nums[i]);
temp.insert((float)nums[i]/nums[j]);
temp.insert((float)nums[j]/nums[i]);
list[t]=temp;
}
}
for(int i=0;i<4;++i)
{
int mask=1<<i;
for(int j=0;j<4;++j)
{
if(i==j)
continue;
int t=mask|1<<j;
set<float> s1=list[t];
int val=(15^t);
set<float> s2=list[val];
for(auto itr1=s1.begin();itr1!=s1.end();++itr1)
for(auto itr2=s2.begin();itr2!=s2.end();++itr2)
{
float a=*itr1+*itr2;
float b=(*itr1)*(itr2);
float c=*itr1-*itr2;
float d=*itr2-*itr1;
float e=(*itr1)/(*itr2);
float f=(*itr2)/(*itr1);
if(abs(a-24.0)<1e-6||abs(b-24)<1e-6||abs(c-24)<1e-6||abs(d-24)<1e-6||abs(e-24)<1e-6||abs(f-24)<1e-6)
return true;
}
}
}
return false;
}