暴力枚举 + 24点 --- hnu : Cracking the Safe
| Cracking the Safe | 
| Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB | 
| Total submit users: 46, Accepted users: 12 | 
| 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. | 
| Output | 
| Per test case: | 
| Sample Input | 
| 4 | 
| Sample Output | 
| YES | 
| Problem Source | 
| BAPC preliminary 2013 | 
Mean:
给你4个数,你需要判断这4个数是否能够通过"+"、"-"、"*"、"/"四种得到24。
analyse:
数据这么小,直接暴力枚举。
先枚举四个数的位置,再枚举三个运算符,最后枚举运算顺序。
解法二:递归求解。
Time complexity:4*4*4*4*4*4*4*5=81920,不超过81920次
Source code:
// 1143K 27MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
bool mark;
double num[];
double aa[];
double calc(double a,double b,int flag)
{
switch(flag)
{
case :return (a+b);
case :return (a-b);
case :return (a*b);
case :return (a/b);
}
}
void algebra()
{
double tmp1,tmp2,tmp3;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
for(int k=;k<=;k++)
{
// 运算顺序1
tmp1=calc(aa[],aa[],i);
tmp2=calc(tmp1,aa[],j);
tmp3=calc(tmp2,aa[],k);
if(fabs(tmp3-)<1e-6)
{
mark=;
return ;
}
// 运算顺序2
tmp1=calc(aa[],aa[],i);
tmp2=calc(aa[],aa[],k);
tmp3=calc(tmp1,tmp2,j);
if(fabs(tmp3-)<1e-6)
{
mark=;
return ;
}
// 运算顺序3
tmp1=calc(aa[],aa[],j);
tmp2=calc(aa[],tmp1,i);
tmp3=calc(tmp2,aa[],k);
if(fabs(tmp3-)<1e-6)
{
mark=;
return ;
}
// 运算顺序4
tmp1=calc(aa[],aa[],j);
tmp2=calc(tmp1,aa[],k);
tmp3=calc(tmp2,aa[],i);
if(fabs(tmp3-)<1e-6)
{
mark=;
return ;
}
// 运算顺序5
tmp1=calc(aa[],aa[],k);
tmp2=calc(aa[],tmp1,j);
tmp3=calc(aa[],tmp2,i);
if(fabs(tmp3-)<1e-6)
{
mark=;
return ;
}
}
}
}
}
void arrange()
{
    for(int i=;i<=;i++)
    {
        for(int j=;j<=;j++)
        {
            if(j==i)continue;
            for(int k=;k<=;k++)
            {
                if(k==i||k==j)continue;
                for(int l=;l<=;l++)
                {
                    if(l==i||l==j||l==k)continue;
                    aa[]=num[i],aa[]=num[j],aa[]=num[k],aa[]=num[l];
                    algebra();
                }
            }
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        mark=false;
        for(int i=;i<=;i++)
            cin>>num[i];
        arrange();
        if(mark)
            puts("YES");
        else
            puts("NO");
    }
    return ;
}
递归:
// 724K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
double num[];
bool solve ( int n ) {
if ( n == ) {
if ( fabs ( num[] - ) < 1E-6 )
return true;
else
return false;
}
for ( int i = ; i < n; i++ ) 
    {
        for ( int j = i + ; j < n; j++ ) 
        {
                    double a, b;
                    a = num[i];
                    b = num[j];
                    num[j] = num[n - ];
                    
                    num[i] = a + b;
                    if ( solve ( n -  ) )
                        return true;
                        
                    num[i] = a - b;
                    if ( solve ( n -  ) )
                        return true;
num[i] = b - a;
                    if ( solve ( n -  ) )
                        return true;
num[i] = a * b;
                    if ( solve ( n -  ) )
                        return true;
                        
                        
                    if ( b !=  ) {
                        num[i] = a / b;
if ( solve ( n -  ) )
                            return true;
                    }
                    if ( a !=  ) {
                        num[i] = b / a;
                        if ( solve ( n -  ) )
                            return true;
                    }
                    num[i] = a;
                    num[j] = b;
        }
    }
    return false;
}
int main() {
    int x;
    int T;
    cin >> T;
    while ( T-- ) {
        for ( int i = ; i < ; i++ ) {
            cin >> x;
            num[i] = x;
        }
if ( solve (  ) ) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
return ;
}
暴力枚举 + 24点 --- hnu : Cracking the Safe的更多相关文章
- HNU 12886 Cracking the Safe(暴力枚举)
		题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ... 
- HNU 12886  Cracking the Safe 二十四点的判断
		经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ... 
- HDU 4770 Lights Against Dudely   暴力枚举+dfs
		又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ... 
- HDU 1015.Safecracker【暴力枚举】【8月17】
		Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is lo ... 
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
		题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ... 
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
		/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ... 
- 51nod 1116 K进制下的大数  (暴力枚举)
		题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ... 
- Codeforces Round #349 (Div. 1)  B. World Tour 最短路+暴力枚举
		题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ... 
- bzoj 1028 暴力枚举判断
		昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ... 
随机推荐
- mongodb(分片)
			分片(即sharding)是将数据拆分至不同数据节点的方式. 1.在mongoDB中提供了自动分片的方式,它会根据数据块(chunk)大小的设定,对片键进行拆分: 2.mongoDB配置分片,要配置三 ... 
- 冲刺阶段 day12
			项目进展 周二我们将专业管理部分又继续做了完善,之前漏掉的几项功能也都在熟能生巧中编写的越来越顺畅,但还差最后一点数据库部分没能实现,我们会尽快完成. 存在问题 还是与数据库的连接上出现问题,部分不能 ... 
- Alpha阶段冲刺项目总结(补充)
			Alpha阶段冲刺阶段总结(补充) 此篇博客为"作业七:Alpha版本冲刺阶段" 与 "作业八:Alpha阶段项目总结" 的总结版. 一.项目预期计划vs实际进 ... 
- 基础调试命令 - wt (watch and trace)
			本文介绍windbg动态调试过程中一个非常有用的命令,wt的用法. wt命令 wt命令之所以称为wt是因为它是watch and trace的简称,即用来观察和跟踪的命令.这个命令一般用在动态调试而不 ... 
- SQL SERVER--DBA 常用到的一些脚本
			自己整理了一些常用到的脚本,希望对各位有用 下载地址 --================================== 妹子不能少,是吧 BTW, 妹子是我辛苦百度来的,请不要求种求介绍各种求 ... 
- 关于node.js的误会
			昨天写了篇博客,介绍了一下我对node.js的第一次亲密接触后的感受,以为node.js很小众,出乎我意料很多人感兴趣,并且对博客中的细节问题做了评论,最多的是围绕node.js的异步与单线程展开的, ... 
- springmvc下js控制表单提交(表单提交前检验,提交后获取json返回值)
			这个问题我搞了四天,终于搞懂.因为对js很不熟悉.郁闷的是后台代码出错总可以设置断点调试,前端js代码出错只能通过浏览器提供一些运行数据来分析,很不习惯. 首先说下逻辑:这是一个注册功能,我希望,注册 ... 
- java 锁3
			先谈线程的状态: 具体来说有, NEW. Running. Blocked.此状态的线程阻塞,它正在等待监视器锁——等待另外一个线程释放锁(通俗说就是等它执行完synchronized了的方法/代码块 ... 
- ASP.net状态服务器使用
			最近在开发一.NET4.0系统时经常发生session丢失问题,导致用户频繁登陆,给客户造成不良的用户体验.应项目经理号召尽快解决此问题. 一.问题描述. 服务器:windows server 200 ... 
- Java程序员的日常 —— 注册工厂的妙用
			注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象. 举个栗子 比如一个家具工厂,有沙发.椅子.茶几等等,正常的编程模式是这样的: //创建 class 沙发{} class 椅子{} ... 
