湖大训练赛8 Cracking the Safe(24点的游戏)
2014-08-10 16:47
344 查看
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 |
直接用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; }
相关文章推荐
- 暴力枚举 + 24点 --- hnu : Cracking the Safe
- HNU 12886 Cracking the Safe(暴力枚举)
- hnu 12886 Cracking the Safe
- 湖大暑假训练赛2:The Stones Game
- 753-Cracking the Safe
- [LeetCode] Cracking the Safe 破解密码
- 问题:No VSS database (srcsafe.ini) found. Use the SSDIR environment variable or run netsetup
- Cracking the coding interview--Q9.7
- Cracking the code interview 1.6
- [IDA*] UVa1343 The Rotation Game 旋转游戏
- Cracking the Coding Interview Q1.2
- cracking the coding interview problem solution 1.3
- Cracking The Coding Interview 5.6
- Java实现24点纸牌游戏
- Cracking the Coding Interview Q2.4
- Cracking the coding interview--Q4.2
- VSS出错"Could not find the Visual SourceSafe Internet Web Service connection..."
- The Safe Navigation Operator (&.) in Ruby
- you *might* want to use the less safe log_bin_trust_function_creators variable
- Cracking the coding interview--Q4.7