HOJ 12886 24点规则 超赞想法

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值