HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274
解题报告:输入4个数,要你判断用 + 、- 、 * 、/、四种运算能不能得到一个结果为24的式子,可以用括号。
解释一下测试的第四组样例:应该是6 / (1 - 3 / 4)
暴力枚举三种符号分别是什么,然后枚举这三种符号运算的顺序,然后枚举这四个数字的24种排列方式,时间是4^3 * 6 * 24
然后注意要用double型,判断是否等于24的时候要这样fabs(ans - 24.0) <= 0.000000001
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
using namespace std;
int fu[];
int mei[][] = {
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
{ , , ,},
};
double CC(int f,double a,double b)
{
if(f == && b == ) return ;
if(f == ) return a + b;
if(f == ) return a - b;
if(f == ) return a * b;
if(f == ) return a / b;
}
double calc(int l,double a,double b,double c,double d)
{
// double a = num[1],b = num[2],c = num[3],d = num[4];
if(l == )
return CC(fu[],CC(fu[],CC(fu[],a,b),c),d);
if(l == )
return CC(fu[],CC(fu[],a,b),CC(fu[],c,d));
if(l == )
return CC(fu[],CC(fu[],a,CC(fu[],b,c)),d);
if(l == )
return CC(fu[],a,CC(fu[],CC(fu[],b,c),d));
if(l == )
return CC(fu[],CC(fu[],a,b),CC(fu[],c,d));
if(l == )
return CC(fu[],a,CC(fu[],b,CC(fu[],c,d)));
}
int main()
{ int T;
scanf("%d",&T);
while(T--)
{
double num[];
scanf("%lf%lf%lf%lf",&num[],&num[],&num[],&num[]);
int flag = ;
for(int i = ;flag && i <= ;++i)
for(int j = ;flag && j <= ;++j)
for(int k = ;flag && k <= ;++k)
{
fu[] = i;
fu[] = j;
fu[] = k;
for(int m = ;flag && m < ;++m)
{
for(int l = ;flag && l <= ;++l)
{
double ans = calc(l,num[mei[m][]],num[mei[m][]],num[mei[m][]],num[mei[m][]]);
if(fabs(ans-24.0) <= 0.00000001)
{
flag = ;
break;
}
}
}
}
printf(flag? "NO\n":"YES\n");
}
return ;
}
HNU 12886 Cracking the Safe(暴力枚举)的更多相关文章
- HNU 12886 Cracking the Safe 二十四点的判断
经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ...
- 暴力枚举 + 24点 --- hnu : Cracking the Safe
Cracking the Safe Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit u ...
- 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, ...
随机推荐
- 自然语言16.1_Python自然语言处理学习笔记之信息提取步骤&分块(chunking)
QQ:231469242 欢迎喜欢nltk朋友交流 http://www.cnblogs.com/undercurrent/p/4754944.html 一.信息提取模型 信息提取的步骤共分为五步,原 ...
- zabbix搭建
首先创建zabbix组和用户 groupadd zabbix useradd -g zabbix zabbix mkdir -p /app/local/zabbix#然后创建zabbix编译安装目录 ...
- super 要点
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } p ...
- Java关键字——throws和throw
throws关键字 在定义一个方法时,可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,而交给方法的调用处进行处理. 使用了throws关键字,表示不管是否会有异常,在调 ...
- 64bit upload app store
Unity将来时:IL2CPP是什么? http://zhuanlan.zhihu.com/indieace/19972689 Unity3D将来时:IL2CPP(下) http://www.game ...
- NopCommerce源码分析ContainerBuilder builder.Update(container)
/// <summary> /// Register dependencies /// </summary> /// <param name="config&q ...
- thinkphp 3.2 视图模型 实例 视图查询结果 二维数组 合并
使用视图模型查询的时候 结果是这样的 array(6) { [0] => array(5) { ["picTitle"] => string(7) "标题2& ...
- Python之闭包
Python之闭包 我们知道,在装饰器中,可以在函数体内创建另外一个函数,例如: def makebold(fn): def wrapped(): return "<b>&quo ...
- [MongoDB]增删改查
摘要 上篇文章学习了mongodb在windows上的安装,以及如何开启mongodb,最后列举了简单的增删改查操作.本篇将继续深入学习一下增删改查. 相关文章 [MongoDB]入门操作 CRUD ...
- [git]添加项目到git
写在前面 一直在想把代码托管到git上面,一直没有去研究,最近发现自己写的demo,好多都找不到了,实在是没办法了,耐下心研究了下git.这里通过添加了自己做的demo,算是也是学习下git的操作吧. ...