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

题意:

给你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 ;
}

最新文章

  1. ucos实时操作系统学习笔记——内核结构和任务创建
  2. 20161022 NOIP模拟赛 解题报告
  3. 在Activity中响应ListView内部按钮的点击事件
  4. MySQL基于mysqldump及lvmsnapshot备份恢复
  5. 百度编辑器umeditor使用总结
  6. Hadoop环境搭建-入门伪分布式配置(Mac OS,0.21.0,Eclipse 3.6)
  7. &lt;&lt;开源硬件创客 15个酷应用玩转树莓派&gt;&gt;
  8. Android Tv 中的按键事件 KeyEvent 分发处理流程
  9. Python argparse模块实现模拟 linux 的ls命令
  10. 自定义 Cordova插件(基础篇)
  11. C# signtool error:no certificates were found that met all the given criteria 错误解决方案
  12. selenium+python谷歌驱动配置
  13. Python中包(package)的调用方式
  14. keras 文本分类 LSTM
  15. arc 092D Two Sequences
  16. Linux下自制回收站
  17. TAC队--团队选题报告
  18. 转 linux下面apache2.0.52+php5+gd2+mysql
  19. tomcat8.5性能优化
  20. Quartz Trigger Priority 触发器优先级

热门文章

  1. SQL之子查询
  2. CDR X6三折促销活动,可入
  3. Dapper基础知识二
  4. [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)
  5. Zookeeper入门-Linux环境下异常ConnectionLossException解决
  6. 平凡主丛上的Yang-Mills理论
  7. svn文件管理器的使用
  8. 洛谷 P2665 [USACO08FEB]连线游戏Game of Lines
  9. 洛谷 P2744 [USACO5.3]量取牛奶Milk Measuring
  10. 三期_day02_数据库表设计和开发准备工作