CSU
来源:互联网 发布:淘宝清库存 编辑:程序博客网 时间:2024/06/05 18:24
Given four numbers, can you get twenty-four through the addition, subtraction, multiplication, and division? Each number can be used only once.
The input consists of multiple test cases. Each test case contains 4 integers A, B, C, D in a single line (1 <= A, B, C, D <= 13).
For each case, print the “Yes” or “No”. If twenty-four point can be get, print “Yes”, otherwise, print “No”.
2 2 3 91 1 1 1 5 5 5 1
YesNoYes
For the first sample, (2/3+2)*9=24.
解题思路:也是参考了大神的代码,因为数据较小,其实可以直接去dfs得出答案
代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;double EPS = 1e-10;double num[4];bool flag;bool equel(double a, double b){ if(fabs(a - b) <= EPS) return true; return false;}void DFS(int n){ if(flag || (n == 1 && equel(num[0], 24))) { flag = true; return; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { double c1 = num[i], c2 = num[j]; num[j] = num[n - 1]; num[i] = c1 + c2; DFS(n - 1); num[i] = c1 - c2; DFS(n - 1); num[i] = c2 - c1; DFS(n - 1); num[i] = c1 * c2; DFS(n - 1); if(!equel(c2, 0)) { num[i] = c1 / c2; DFS(n - 1); } if(!equel(c1, 0)) { num[i] = c2 / c1; DFS(n - 1); } num[i] = c1; num[j] = c2; } }}int main(){ while(scanf("%lf %lf %lf %lf", &num[0], &num[1], &num[2], &num[3]) != EOF) { flag = false; DFS(4); printf("%s\n", flag ? "Yes" : "No"); }}
阅读全文
0 0
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- 大学英语单词F
- subset
- Python 字符串操作方法大全
- 解析json数据
- 人生第一篇博客,写在这里
- CSU
- 2017 Multi-University Training Contest
- A
- python高阶函数中的map、filter、reduce
- 【Machine Learning】笔记:Logistic Regression Model
- html编辑器
- 百度鹰眼地图使用
- OpenGL ES/Open CV/OpenGL、OpenCL与OpenAL研究
- Android性能优化之布局优化(一)