poj 2787
来源:互联网 发布:淘宝宝贝倒计时生成器 编辑:程序博客网 时间:2024/06/17 01:42
#include <iostream>#include <cmath>using namespace std;const int NUM = 4;const int TARGET = 24;const double ZERO = 1e-6;bool flag = false;double number[NUM];bool Deal(int);int main(){ while(cin >> number[0]) { if(fabs(number[0] - 0.0) < ZERO) break; for(int i = 1; i < NUM; ++i) cin >> number[i]; flag = false; Deal(NUM); if(flag) cout << "YES" << endl; else cout << "NO" << endl; } return 0;}bool Deal(int step){ if(step == 1) { if(fabs(number[0] - TARGET) < ZERO) { flag = true; return true; } else return false; } for(int i = 0; i < step; ++i) { for(int j = i + 1; j < step; ++j) { double tmp_a = number[i], tmp_b = number[j]; number[j] = number[step - 1]; number[i] = tmp_a + tmp_b; Deal(step - 1); number[i] = tmp_a - tmp_b; Deal(step - 1); number[i] = tmp_b - tmp_a; Deal(step - 1); number[i] = tmp_a * tmp_b; Deal(step - 1); if(fabs(tmp_b - 0.0) > ZERO) { number[i] = tmp_a / tmp_b; Deal(step - 1); } if(fabs(tmp_a - 0.0) > ZERO) { number[i] = tmp_b / tmp_a; Deal(step - 1); } number[i] = tmp_a; number[j] = tmp_b; } } return true;}
这道题是参考了别人的代码写的,之前写的那个太麻烦了,简直直逼枚举代码长度
看了之后才发现可以用double的精度,同时调整运算顺序只需一个循环即可~
0 0
- poj 2787
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 电脑命令大全集
- Smarty 学习
- Interface - phantomjs - API Reference - headless WebKit with JavaScript API - Google Project Hosting
- 数据结构实验:哈希表
- s3c2440对nandflash的操作
- poj 2787
- java版泰囧表情的生成器代码,不用PS也能恶搞。
- HDU1251 trie(结构体)
- 数据结构实验:哈希表
- jsp页面中的EL表达式不被解析的问题
- tftp连接时出线retry count exceeded starting again时检查步骤
- 在Code上创建了新的项目TEngine
- Cocoapods Error "Pull is not possible because you have unmerged files." #1268
- android重启代码