经典的一个题,今天竟然写跪了……

题意:

给你4个数字,让你判断是否能通过四则运算和括号,凑成24点。

思路:

暴力枚举运算顺序和运算符。

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <time.h> using namespace std; const int INF = <<;
const double eps = 1e-; double a[];
inline double myAbs(double x) {
return x> ? x : -x;
} bool dfs(double S[], int cnt) { //S[]中存放当前还有的数字
//计算方法: 每次从剩下的数字里取出两个数运算,相当于枚举运算顺序
if (cnt==) //最后一个数字
return myAbs(-S[])<eps; double b[];
for (int i = ; i < cnt; i++) //枚举两个运算的数字
for (int j = ; j < cnt; j++) if (i!=j) {
for (int k = , p = ; k < cnt; k++) {
if (k!=i&&k!=j)
b[p++] = S[k];
}
//枚举运算
b[] = S[i]+S[j];
if (dfs(b, cnt-)) return true;
b[] = S[i]-S[j];
if (dfs(b, cnt-)) return true;
b[] = S[i]*S[j];
if (dfs(b, cnt-)) return true;
b[] = S[i]/S[j];
if (dfs(b, cnt-)) return true;
}
return false;
} void solve() {
do { //枚举排列
if (dfs(a, )) {
puts("YES");
return ;
}
}while (next_permutation(a, a+));
puts("NO");
} int main() {
#ifdef Phantom01
freopen("HNU12879.txt", "r", stdin);
#endif //Phantom01 int T;
scanf("%d", &T);
while (T--) {
for (int i = ; i < ; i++)
scanf("%lf", &a[i]);
solve();
} return ;
}

HNU 12886 Cracking the Safe 二十四点的判断的更多相关文章

  1. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  2. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  3. 二十四点算法 java实现

    问题: 给出四个数,不可以重复使用,可以用+ - * /和括号,怎么得出24? 代码: //return -1 表示当前方法不行 private int workByStep(int op,int n ...

  4. 第十六次 ccf 201903-2 二十四点

    题意: 计算数学表达式的值, 数学表达式的定义: 4个[0-9]表示数字的字符 ,3个[+-x/]表示运算的字符 可以用正则为: ([0-9][+-x/]){3}[0-9] 例如: 5+2/1x3 2 ...

  5. CCF-CSP题解 201903-2 二十四点

    可枚举. 写栈的主要思想是:一个数栈\(numSta\),一个运算符栈\(opSta\).遇到一个运算符,就把之前优先级\(equal\ or\ greater\ than\)它的运算符处理掉. #i ...

  6. 201903-2 CCF 二十四点

    题面: 考场写的30分== #include<bits/stdc++.h> using namespace std; stack<int>st; stack<char&g ...

  7. CSP201903-2二十四点

    如图所示先处理乘号和除号,再处理加减. #include<bits/stdc++.h> using namespace std; ];int main(){ int n; cin>& ...

  8. 201903-2 二十四点 Java

    思路: 数据结构中,栈可以解决运算的问题.利用压栈和弹栈操作实现(这里用队列模拟).具体的: 遇到乘除号,弹出栈顶元素,将计算结果压入栈中.遇到加减号,将后面的数一起压入栈中. 注意: substri ...

  9. CCF201903-2二十四点

    思路描述:最开始的思路是拿一个栈来存储数据和符号,在动手实践的过程中发现行不通,单个数字的char和int转换可以,但是加起来的数据两位数字就很难处理了. 然后就去看了看别人的思路,给了我一个很好的启 ...

随机推荐

  1. 电信流氓注入JS

    (function () { var cs_url = _pushshowjs_.url, cs_delay = window.cs_delay; var cs_styles = window.sty ...

  2. Servlet学习(七)——cookie

    一.会话技术简介 1.存储客户端的状态 例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术 ...

  3. jQuery学习(八)——使用JQ插件validation进行表单校验

    1.官网下载:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 目录结构: 2.引入jquery库和validation插件 ...

  4. 简洁的MVC思想框架——Nancy(Session的使用)

    前文提到关于Nancy中GET和POST以及外部引用图片,css和JS的文件等操作.今天所讲的是Nancy关于Session相关操作. Session作为web开发中极其重要的一部分,而Nancy中S ...

  5. C语言基础 (3) C语言介绍

    01回顾 02 语言介绍 语言是人和人交流,C语言是人和机器交流. 03_为什么学C语言 04_第一个C代码编译运行 #include <stdio.h> int main() { // ...

  6. Centos7&docker-ce&compose&wordpress

    如题,最近帮人装个WordPress,想起来用docker方便,这里做个记录. 原文:https://my.oschina.net/finchxu/blog/2877580 因为docker要求lin ...

  7. Linux学习总结(11)——Linux文件查找

    Linux下的常用查找命令 locate whereis which find locate  -i, 忽略大小写  find  根据文件名或正则表达式搜索  -name    条件限制  -a 与条 ...

  8. Qt之QSS(动态属性)

    简述 QSS可以定制应用程序的外观,无需关注Qt样式背后的魔力.从非常轻微到极其复杂的调整,样式表都可以做到.对于一个完全定制和独特的用户体验,QtQuick和QGraphicsView是更好的选择. ...

  9. gdb与信号

    http://simohayha.iteye.com/blog/493091 gdb可以监测在你的程序中的任何信号. 主要靠的命令是: handle signal [keywords...] 这里的k ...

  10. Step by Step Do IOS Swift CoreData Simple Demo

    简单介绍 这篇文章记录了在 IOS 中使用 Swift 操作 CoreData 的一些基础性内容,因为缺乏文档,基本上都是自行实验的结果.错漏不可避免,还请谅解. 部分内容借鉴了 Tim Roadle ...