湖大训练赛8 Cracking the Safe(24点的游戏)

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.
A valid key satisfies a certain condition, which we call the 24 condition. Four digits that satisfy the 24 condition can be manipulated using addition, subtraction, multiplication, division and parentheses, in such a way, that the end result equals 24.
For example, the key (4; 7; 8; 8) satisfies the 24 condition, because (7-8/8)×4 = 24. The key (1; 1; 2; 4) does not satisfy the 24 condition, nor does (1; 1; 1; 1). These keys cannot possibly be the valid key and do not need to be tried.
Write a program that takes the list of possible keys and outputs for each key whether it satisfies the 24 condition or not.


Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:
one line with four space-separated integers a; b; c; d (1<=a; b; c; d<=9): a possible key.


Output

Per test case:
one line with either “YES” or “NO”, indicating whether the key satisfies the 24 condition or not.


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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值