-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path算24.cpp
More file actions
73 lines (73 loc) · 1.12 KB
/
算24.cpp
File metadata and controls
73 lines (73 loc) · 1.12 KB
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
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cmath>
#define EPS 1e-6
#define N 4
using namespace std;
double a[N];
bool isZero(double x){
return fabs(x)<=EPS;
}
bool count24(double a[],int n)
{
if(n==1){
if(isZero(a[0]-24))
return true;
else
return false;
}
double b[N];
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
int m=0;
for(int k=0;k<n;k++)
{
if(k!=i&&k!=j)
b[m++]=a[k];
}
b[m]=a[i]+a[j];
if(count24(b,m+1))
return true;
b[m]=a[i]-a[j];
if(count24(b,m+1))
return true;
b[m]=a[j]-a[i];
if(count24(b,m+1))
return true;
b[m]=a[i]*a[j];
if(count24(b,m+1))
return true;
if(!isZero(a[j])){
b[m]=a[i]/a[j];
if(count24(b,m+1))
return true;
}
if(!isZero(a[i])){
b[m]=a[j]/a[i];
if(count24(b,m+1))
return true;
}
}
return false;
}
int main()
{
int t=1;
while(t){
t=N;
for(int i=0;i<N;i++){
cin>>a[i];
if(a[i]==0)
--t;
}
while(t){
if(count24(a,N)){
cout<<"YES"<<endl;break;
}
else {
cout<<"NO"<<endl;break;
}
}
}
return 0;
}