百炼2787
来源:互联网 发布:法国人接吻 知乎 编辑:程序博客网 时间:2024/04/30 01:42
1.题目描述:
这里加减乘除以及括号的运算结果和运算的优先级跟我们平常的定义一致(这里的除法定义是实数除法)。
比如,对于5,5,5,1,我们知道5 * (5 – 1 / 5) = 24,因此可以得到24。又比如,对于1,1,4,2,我们怎么都不能得到24。
5 5 5 11 1 4 20 0 0 0
YESNO3.解题思路:
dfs对每个数进行 + - * / 运算,注意除0情况。因为有除法还要注意精度损失问题。可以看到,枚举类的问题,如果数据客观,都可以尝试dfs枚举+回溯来解决。
4.AC代码:
#include <stdio.h>#include <math.h>#include <string.h>#define eps 1e-6#define N 4using namespace std;double num[N];int flag;void dfs(int step){if (step == 1){if (fabs(num[0] - 24) < eps)flag = 1;return;}for (int i = 0; i < step; i++){for (int j = i + 1; j < step; j++){double a = num[i], b = num[j];num[j] = num[step - 1];num[i] = a + b;dfs(step - 1);num[i] = a - b;dfs(step - 1);num[i] = a * b;dfs(step - 1);num[i] = b - a;dfs(step - 1);if (fabs(b - 0.0) > eps){num[i] = a / b;dfs(step - 1);}if (fabs(a - 0.0) > eps){num[i] = b / a;dfs(step - 1);}num[i] = a;num[j] = b;}}}int main(){while (scanf("%lf%lf%lf%lf", &num[0], &num[1], &num[2], &num[3]) && num[0] + num[1] + num[2] + num[3]){flag = 0;dfs(4);if (flag)puts("YES");elseputs("NO");} return 0;}
0 0
- 百炼2787
- 百炼
- 百炼采药问题....
- 百炼 1833:排列
- 百炼-2726:采药
- poj百炼 2788
- 【LIS】百炼-2757
- 百炼OJ1001
- 百炼OJ1002
- 百炼OJ1003
- 百炼OJ1004
- 百炼OJ1005
- 百炼OJ1006
- 百炼OJ1007
- 百炼OJ1009
- 百炼 Zipper 剪枝
- 百炼 2804 词典
- OpenJudge百炼-2787-算24-C语言-枚举
- Java 集合SortedSet&SortedMap讲解
- 走进 Redis 的世界
- BLE启动加密和暂停加密规程
- Node.js 模块之【passport】第三方【Github】鉴权(二)
- L2-001. 紧急救援
- 百炼2787
- windows 7 激活工具
- 如何将eclipse下R3-QUery源码项目导入到Intellij idea部署运行
- 设计模式--Singleton(转自http://cantellow.iteye.com/blog/838473)
- Java EE基础知识学习(二)
- LintCode | 93. 平衡二叉树
- System.exit()用法
- <Effective JavaScript>
- windows下揪出java程序占用cpu很高的线程 并找到问题代码 死循环线程代码