暴力枚举 + 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, ...
随机推荐
- WIX 安装部署教程(六) 为你收集的七个知识点
前段时间整理5篇WIX(Windows Installer XML)的安装教程,但还不够完善,这里继续整理了七个知识点分享给大家.WIX最新版本3.8,点击下载 WIX安装部署(一)同MSBuild自 ...
- [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式
[.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式 说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如 ...
- 译文---C#堆VS栈(Part Four)
前言 在本系列的第一篇文章<C#堆栈对比(Part Three)>中,介绍了值类型和引用类型在Copy上的区别以及如何实现引用类型的克隆以及使用ICloneable接口等内容. 本文为文章 ...
- 在代码中设置IE9的默认文档模式
要在旧系统中加一个jquery插件,本地demo测试没问题,部署到服务器后却报错.使用的是IE9浏览器,打开F12调试台,发现默认的文档模式是IE7,调成IE9后,报错消失.可以确认是该插件不兼容IE ...
- [翻译]AKKA笔记 - ACTOR MESSAGING - REQUEST AND RESPONSE -3
上次我们看Actor消息机制,我们看到开火-忘记型消息发出(意思是我们只要发个消息给Actor但是不期望有响应). 技术上来讲, 我们发消息给Actors就是要它的副作用. 这就是这么设计的.除了不响 ...
- Springlake-01 介绍&功能&安装
1. 简介与功能 1)Springlake 是一个企业内容平台SECP 2)是一个可配置的系统,80%内容可以配置 3)允许建立和配置垂直解决方案 4)敏捷和占用空间小,可伸缩 5)端到端的安全性与性 ...
- MVVM架构~knockoutjs系列之正则表达式使规则更灵活
返回目录 几乎每种验证架构都会有正则表达式的加盟,一般地,一种验证架构首先会提供一些标准的,常用的验证规则,它们通常是数字验证,电话验证,email验证,长度验证,范围验证,日期验证等,而如果使你的验 ...
- Atitit图像识别的常用特征大总结attilax大总结
Atitit图像识别的常用特征大总结attilax大总结 1.1. 常用的图像特征有颜色特征.纹理特征.形状特征.空间关系特征. 1 1.2. HOG特征:方向梯度直方图(Histogram of O ...
- vue.js学习之入门实例
之前一直看过vue.js官网api,但是很少实践,这里抽出时间谢了个入门级的demo,记录下一些知识点,防止后续踩坑,牵扯到的的知识点:vue.vue-cli.vue-router.webpack等. ...
- ngOptions
ngOptions select as select as label for value in array <select ng-model="myColor" ng-op ...