-
Notifications
You must be signed in to change notification settings - Fork 8
/
#1304 搜索一·24点AC.cpp
79 lines (66 loc) · 1.63 KB
/
#1304 搜索一·24点AC.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
using namespace std;
double number[4];
double nowNumber[4];
char ops[3];
char opType[4] = { '+', '-', '*', '/' };
bool used[4]={false};
int num;
double calc(double a, double b, char c){
double s = 0.0;
switch (c)
{
case '+': s = a + b;break;
case '-': s = a - b;break;
case '*': s = a*b;break;
case '/': if (b != 0) s = b / a;break;
default: break;
}
return s;
}
bool makeOperation(int depth){
if(depth>=3){
if ( calc(calc(number[0], number[1],ops[0]), calc(number[2], number[3],ops[1]),ops[2]) == 24 //(A#B)#(C#D)
|| calc(calc(calc(number[0],number[1],ops[0]),number[2],ops[1]),number[3],ops[2]) == 24 //((A#B)#C)#D
|| calc(number[0], calc(number[1], calc(number[2], number[3], ops[2]), ops[1]), ops[0]) == 24 //A#(B#(C#D))
|| calc(calc(number[0], calc(number[1], number[2], ops[1]), ops[0]), number[3], ops[2]) == 24 //(A#(B#C))#D
|| calc(number[0], calc(calc(number[1], number[2], ops[1]), number[3], ops[2]), ops[0]) == 24 //A#((B#C)#D)
)
return true;
else
return false;
}
for(int i=0;i<4;i++){
ops[depth] = opType[i];
if (makeOperation(depth + 1))
return true;
}
return false;
}
bool makeNumber(int depth){
if(depth>=4)
return makeOperation(0);
for(int i=0;i<4;i++){
if(!used[i]){
nowNumber[depth]=number[i];
used[i]=false;
if(makeNumber(depth+1))
return true;
used[i]=false;
}
}
return false;
}
int main(){
int n;
cin>>n;
while(n--){
for (int i = 0; i < 4; i++)
cin >> number[i];
if (makeNumber(0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}