Cracking the Safe |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 20, Accepted users: 14 |
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 |
这个题意很简单。给你四个数,和加减乘除括号几个运算法则,问他们能不能变成24.
直接用next_permutation()函数把这四个数序列都进行枚举。然后对符号进行枚举3*3*3,然后对符号运算顺序进行枚举5次(有人可能说6次,132和312都一样。然后判断是否等于24.这个题目我在 double型这里坑了好久。因为有除法,可能会是小说形式。所以我们运算和比较的时候用double类型计算比较的规则。fabs(a-b)<=0.00000001吧。然后就AC了,好坑爹啊。不过说实话,这个游戏挺好玩的,哈哈哈。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
#define ling 0.0000000001
double go(double a,double b,int c)
{
if (c==0) return a+b;
else if (c==1) return a-b;
else if (c==2) return a*b;
//else if (b==0) return 10000;
else return a/b;
}
double a[5],b[5];
int T,i,t,j,k;
int main()
{
scanf("%d",&T);
while (T--)
{
for (i=0;i<4;i++)
scanf("%lf",&a[i]);
sort(a,a+4);
int f=0;
do
{
double ans=0;
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
for (k=0;k<4;k++)
{
memset(b,0,sizeof(b));
b[1]=go(a[0],a[1],i);
b[2]=go(b[1],a[2],j);
ans=go(b[2],a[3],k);
if (fabs(ans-24)<=ling)
{
puts("YES");
f=1;
if (f)break;
}
memset(b,0,sizeof(b));
b[1]=go(a[0],a[1],i);
b[3]=go(a[2],a[3],k);
ans=go(b[1],b[3],j);
if (fabs(ans-24)<=ling)
{
puts("YES");
f=1;
if (f)break;
}
memset(b,0,sizeof(b));
b[2]=go(a[1],a[2],j);
b[1]=go(a[0],b[2],i);
ans=go(b[1],a[3],k);
if (fabs(ans-24)<=ling)
{
puts("YES");
f=1;
if (f)break;
}
memset(b,0,sizeof(b));
b[2]=go(a[1],a[2],j);
b[3]=go(b[2],a[3],k);
ans=go(a[0],b[3],i);
if (fabs(ans-24)<=ling)
{
puts("YES");
f=1;
if (f)break;
}
memset(b,0,sizeof(b));
b[3]=go(a[2],a[3],k);
b[2]=go(a[1],b[3],j);
ans=go(a[0],b[2],i);
if (fabs(ans-24)<=ling)
{
puts("YES");
f=1;
if (f)break;
}
}
if (f)break;
}
if (f)break;
}
if (f)break;
}while (next_permutation(a,a+4));
if (f==0) puts("NO");
/*else
{
cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
cout<<i<<j<<k<<endl;
}*/
}
return 0;
}