Cracking the Safe |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 23, Accepted users: 18 |
Problem 12886 : No special judgement |
Problem description |
Secret agent Roger is trying to crack a safe containing evil Syrian chemical weapons. In order to crack the safe, Roger needs to insert a key into the safe. The key consists of four digits. Roger has received a list of possible keys from his informants that he needs to try out. Trying out the whole list will take too long, so Roger needs to find a way to reduce the list. |
Input |
On the first line one positive number: the number of test cases, at most 100. After that per test case: |
Output |
Per test case: |
Sample Input |
4 4 7 8 8 1 1 2 4 1 1 1 1 1 3 4 6 |
Sample Output |
YES NO NO YES |
Problem Source |
BAPC preliminary 2013 |
给四个整数,判断通过 + - * / ()能否构成等于24。对于一个案例 1 3 4 6 ,超难想到怎么构成24,想了个草鸡好笑的结构 1/3+4*6==24因为把他们当整数处理了,可是万万没想到:6 / ( 1 - 3 / 4 ) == 24,太巧了,太赞了。然后,做法就是嘛:让4个里面随意选2个组合在一起得到一个结果,变成3个数了,在挑两个组合在一起,得到2个数了,最后2个组合成了一个最后结构,判断是否等于24就ok了,这样还避免了()的影响。一般做法,4个数的全排列嘛,符号的全排列嘛,不如之前说的巧。做法递归,递归!!!最后注意要精度处理。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define rt return
#define sf scanf
#define pf printf
#define si(n) sf("%d",&n)
#define pi(n) pf("%d",n)
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=s;i<=(n);i++)
#define db double
#define pb push_back
#define LL long long
#define INF 0x3fffffff
#define eps 1e-8
#define PI acos(-1)
#define maxn
using namespace std;
int sig(double x){return (x>eps)-(x<-eps); }
bool dfs(double *a,int len){
if(len==1){if(sig(a[0]-24)==0)return true; }
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
if(i==j)continue;
double b[4];
int l=0;
for(int k=0;k<len;k++){
if(k!=i&&k!=j)
b[l++]=a[k];
}
b[l++]=a[i]+a[j];
if(dfs(b,l)==true)return true;
b[l-1]=a[i]-a[j];
if(dfs(b,l)==true)return true;
b[l-1]=a[i]*a[j];
if(dfs(b,l)==true)return true;
if(sig(a[j]-0)!=0){
b[l-1]=a[i]/a[j];
if(dfs(b,l)==true)return true;
}
}
}
return false;
}
int main(){
#ifdef ACBang
// freopen("in.txt","r",stdin);
#endif
double a[4];
int n;sf("%d",&n);
while(n--){
sf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3]);
int len=4;
if(dfs(a,len)==true)pf("YES\n");
else pf("NO\n");
}
rt 0;
}