暴力枚举 + 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, ...
随机推荐
- 谈谈对BPM的理解
BPM的产生缘由 近年来,随着计算机技术的发展和互联网时代的到来,我们已经进入了信息时代,也称为数字化时代,在这数字化的时代里,企业的经营管理都受到了极大的挑战.从上世纪90年代起至今,企业的信息化工 ...
- [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决
采用sudo启动的应用是root权限的应用, ibus失效是因为ibus的初始配置采用user权限: 而root下运行的firefox输入法的配置还是停留在默认情况~ 解决方案是在shell下以roo ...
- [nRF51822] 1、一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO
最近用nRF51822写了个天马4线SPI的1.77寸LCD彩屏驱动,效果如下: 屏幕的规格资料为:http://pan.baidu.com/s/1gdfkr5L 屏幕的驱动资料为:http://pa ...
- [转]在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
http://cankeyyin.blog.163.com/blog/static/12336178320124149391202/ 原理:将iphone的hd图片给ipad用,即: 使用原iphon ...
- js模版引擎handlebars.js实用教程——循环中使用索引
<!DOCTYPE html> <html> <head> <META http-equiv=Content-Type content="text/ ...
- HTML学习入门
HTML(元素.属性) HTML: 超文本标记语言 1. 超文本即为带有链接属性的文本 2.标记即为标签 一.body属性: bgcolor:页面背景颜色 text:文字颜色 backgroun ...
- php将文件转换成二进制输出[转]
header( "Content-type: image/jpeg"); $PSize = filesize('1.jpg'); $picturedata = fread(fope ...
- Android开发学习之路-Service和Activity的通信
在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...
- MyBatis学习总结(七)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- Android笔记——Android五大布局
一.五大布局 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是Li ...