HNU 12886 Cracking the Safe 二十四点的判断
经典的一个题,今天竟然写跪了……
题意:
给你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 二十四点的判断的更多相关文章
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- [LeetCode] 24 Game 二十四点游戏
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- 二十四点算法 java实现
问题: 给出四个数,不可以重复使用,可以用+ - * /和括号,怎么得出24? 代码: //return -1 表示当前方法不行 private int workByStep(int op,int n ...
- 第十六次 ccf 201903-2 二十四点
题意: 计算数学表达式的值, 数学表达式的定义: 4个[0-9]表示数字的字符 ,3个[+-x/]表示运算的字符 可以用正则为: ([0-9][+-x/]){3}[0-9] 例如: 5+2/1x3 2 ...
- CCF-CSP题解 201903-2 二十四点
可枚举. 写栈的主要思想是:一个数栈\(numSta\),一个运算符栈\(opSta\).遇到一个运算符,就把之前优先级\(equal\ or\ greater\ than\)它的运算符处理掉. #i ...
- 201903-2 CCF 二十四点
题面: 考场写的30分== #include<bits/stdc++.h> using namespace std; stack<int>st; stack<char&g ...
- CSP201903-2二十四点
如图所示先处理乘号和除号,再处理加减. #include<bits/stdc++.h> using namespace std; ];int main(){ int n; cin>& ...
- 201903-2 二十四点 Java
思路: 数据结构中,栈可以解决运算的问题.利用压栈和弹栈操作实现(这里用队列模拟).具体的: 遇到乘除号,弹出栈顶元素,将计算结果压入栈中.遇到加减号,将后面的数一起压入栈中. 注意: substri ...
- CCF201903-2二十四点
思路描述:最开始的思路是拿一个栈来存储数据和符号,在动手实践的过程中发现行不通,单个数字的char和int转换可以,但是加起来的数据两位数字就很难处理了. 然后就去看了看别人的思路,给了我一个很好的启 ...
随机推荐
- rsyslog 存储到 mysql
输出Host1/2的系统日志, 记录到mysql服务器数据库中, 并发布loganalyzer 结构关系如下图: 思路: 通过远程连接mysql, 使得rsyslog的log记录能够写入到mysql中 ...
- 路飞学城Python-Day13
[2.常用模块-模块的种类和导入方法] 1.什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长.越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分 ...
- c/s结构的自动化——pyautogui
环境:Python 3.5.3 pip install pyautogui -i http://pypi.douban.com/simple --trusted-host pypi.douban.co ...
- Vue 基础篇
Vue 基础篇 一.框架与库的区别 JQ库->DOM(DOM操作) + Ajax请求 art-template库->模板引擎 框架 -> 全方位.功能齐全 简易的DOM体验 + 发请 ...
- python做的 QQ未读消息图像
#!/usr/bin/pythonfrom PIL import Image ,ImageDraw, ImageFont#打开所在的文件im=Image.open('test.jpg')#获取图片对象 ...
- 紫书 习题 8-23 UVa 1623 (set妙用 + 贪心)
这道题我是从样例中看出思路了 2 4 0 0 1 1 看这组数据, 输出的是No, 为什么呢?因为两个1之间没有神龙喝水, 所以一定会有水灾. 然后就启发了我,两次同一个湖的降水之间必须至少有一次神龙 ...
- BNUOJ 34990 Justice String
Justice String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cla ...
- java实现上传图片
1.将图片上传到tomcat下 2.将相对路径存放到数据库中 @RequestMapping(params="upLoadPicture") @ResponseBody publi ...
- SQL Server 2008 备份数据库
1.打开SQL , 找到要备份的数据库 , 右键 >> 任务 >>备份 2.弹出 [ 备份数据库对话框 ] ,如图: 3.点击加入 [ button ] . 例如以下图: 4. ...
- oracle新手随记10
1. unpivot注意点:select new_col from (select ename,job,to_char(sal) as sal,null as c from emp) ...