-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path算24
200 lines (161 loc) · 4.37 KB
/
算24
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
http://bailian.openjudge.cn/practice/2787/submit/
ANS:
//my answer
//输入部分表达不清楚 //结果显示wrong ans
#include <iostream>
#include <cmath>
using namespace std;
double a[5];
string s;
const double d = 1e-10;
bool isZero(double n){
return fabs(n) < d;
}
bool count24(double a[], int n){
if(n==1){
if(isZero(a[0]-24)){//return value of 24 --> successful
return true;
}else{
return false;
}
}
double b[5];//store rest variables
//get random 2 variables use random one of four operations
for(int i = 0; i<n; i++){
for(int j=i+1; j<n; j++){
int n_rest = 0;
for(int w = 0; w<n; w++){
if(w!=i && w!=j){
b[n_rest++] = a[w];
}
}
//+
b[n_rest] = a[i] + a[j];
if(count24(b, n_rest+1))
return true;
//- //two conditions
b[n_rest] = a[i] - a[j];//Update value of b[m]
if(count24(b,n_rest+1))
return true;
b[n_rest] = a[j] - a[i];
if(count24(b, n_rest+1))
return true;
//*
b[n_rest] = a[i]*a[j];
if(count24(b, n_rest+1))
return true;
//÷ //two conditions
if(!isZero(a[j])){//not zero for division
b[n_rest] = a[i]/a[j];
if(count24(b, n_rest+1))
return true;
}
if(!isZero(a[i])){
b[n_rest] = a[j]/a[i];
if(count24(b,n_rest+1))
return true;
}
}
}
return false;
}
int main(){
while(1){
getline(cin,s);//string 遇到空格会结束 所以不用cin>>s, 用getline(cin,s);
int j = 0;
for(int i=0; i<s.length(); i++)
{
if(isdigit(s[i])){
a[j] = s[i]-'0';//string to int
j++;
}
}
bool b = 1;
for(auto &r : a){
b &= isZero(r);
}
if(b){//Stop
break;
}
//Not stop //process the variables
if(count24(a,4))//has found
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
//换了输入方式后ok
//提供的答案
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
double a[5];
#define EPS 1e-6
//因为两数相除可能为小数 而浮点型是不能直接用 == 进行比较的 所以用两数的差的绝对值与13-6进行比较小于则相等
bool isZero(double x)
{
return fabs(x) <= EPS;
}
bool count24(double a[], int n)
{
double b[5];
//当数组a里面的元素只剩一个的时候 对a[0]进行判断
if(n == 1)
{
if(isZero(a[0] - 24))
return true;
else
return false;
}
else{
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数组
}
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[j] * a[i] ; //将两数积进行递归
if(count24(b,m+1))
return true;
if(a[i] != 0){ //将两数差进行递归
b[m] = a[j]/a[i];
if(count24(b,m+1)) return true;
}
if(a[j] != 0){ //将两数差进行递归
b[m] = a[i]/a[j];
if(count24(b,m+1)) return true;
}
}
}
}
return false ;
}
int main()
{
int z = 0;
while(1){
for(int i =0 ;i < 4; ++i){
cin >> a[i]; //自动省略空格读取
if(a[i] == 0) z++;//算是不是4个都是0
}
if(z == 4) break;
if(count24(a,4)){
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;
z = 0;
}
return 0;
}