暴力枚举 + 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, ...
随机推荐
- 【重磅推荐】腾讯Bugly2015年移动应用质量大数据报告
2015年,随着移动智能设备的普及,移动端用户的增速明显放缓:相比之下,由于云服务.众筹平台.推广平台等基础设施和服务的不断改善,极大降低了创业的门槛,越来越多人投身于移动应用的创新创业中. 想让用户 ...
- UWP开发随笔——使用SQLite数据库
摘要 大多数的app都需要数据存储,在数据存储这方面,强大的windows把app数据分为两种:settings和files,并提供了十分简洁的api,让开发者能够轻松使用.但是在有些场景下,app的 ...
- 人人都是 DBA(IV)SQL Server 内存管理
SQL Server 的内存管理是一个庞大的主题,涉及特别多的概念和技术,例如常见的 Plan Cache.Buffer Pool.Memory Clerks 等.本文仅是管中窥豹,描述常见的内存管理 ...
- 浅析Ajax跨域原理及JQuery中的实现分析
AJAX 的出现使得网页可以通过在后台与服务器进行少量数据交换,实现网页的局部刷新.但是出于安全的考虑,ajax不允许跨域通信.如果尝试从不同的域请求数据,就会出现错误.如果能控制数据驻留的远程服务器 ...
- C#Light V0.08A 执行字符串中的C#
C#Lite第一批规划的功能已经全部完成 V0.08A 加入了var 表达式支持 完成了类型静态函数和静态成员的访问 之前已完成了非静态的 之所以还不进入beta是临时决定第一批就加入数组的功能. 项 ...
- 赴美工作常识(Part 4 - 面试)
最近跟同事讨论面试的事情比较多,所以就综合大家所说的列举几条面试建议吧.这些建议是针对中国候选人应聘美国职位而写的,但适用范围可能更广.假若你实际的实力是 X,面试官感知到你的实力是 Y,这些建议既不 ...
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes
[我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes 本篇分享一下第5个已完工的视频,即<beginner Graphics – ...
- Java中测试异常的多种方式
使用JUnit来测试Java代码中的异常有很多种方式,你知道几种? 给定这样一个class. Person.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一
回到目录 本讲是通过DbCommand拦截器来实现读写分离的最后一讲,对之前几篇文章做了一个优化,无论是程序可读性还是实用性上都有一个提升,在配置信息这块,去除了字符串方式的拼接,取而代之的是sect ...
- Atitti 知识图谱构建方法attilax 总结
Atitti 知识图谱构建方法attilax 总结 1.1. 知识图谱schema构建(体系化)1 1.2. 纵向垂直拓展(向上抽象,向下属性拓展)2 1.3. 横向拓展2 1.4. 网拓展2 1 ...