P1236 算24点
#include <bits/stdc++.h>
using namespace std;
int b[4];
int a[3];
int calc(int a, int b, int c) {
if(c == 1) return a + b;
else if(c == 2) return a - b;
else if(c == 3) return a * b;
else if(c == 4) {
if(a >= b && a % b == 0) return a/b;
else return -1;
}
}
char trans[] = {'0', '+', '-', '*', '/'};
bool dfs(int cur) {
if(cur == 3) {
int one = calc(b[0], b[1], a[0]);
int two = calc(one, b[2], a[1]);
int three = calc(two, b[3], a[2]);
if(one != -1 && two!=-1 && three!=-1 && three == 24) return true;
else return false;
}
for(int i = 1; i <= 4; i++) {
a[cur] = i;
if(dfs(cur+1)) return true;
}
return false;
}
int main(int argc, char const *argv[])
{
cin >> b[0] >> b[1] >> b[2] >> b[3];
sort(b, b+4);
bool x;
do{
if(x = dfs(0)) break;
} while(next_permutation(b, b+4));
if(x) {
int one = calc(b[0], b[1], a[0]);
int two = calc(one, b[2], a[1]);
int three = calc(two, b[3], a[2]);
cout << max(b[0], b[1]) << trans[a[0]] << min(b[0], b[1]) << '=' << one << endl;
cout << max(one, b[2]) << trans[a[1]] << min(one, b[2]) << '=' << two << endl;
cout << max(two, b[3]) << trans[a[2]] << min(two, b[3]) << '=' << three << endl;
}
else cout << "No answer!";
return 0;
}
P1236 算24点的更多相关文章
- 洛谷P1236 算24点
题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为“算24点”.您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数进行适当的算术运算, ...
- 洛谷 P1236 算24点
题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为"算24点".您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数 ...
- 洛谷【P1236】算24点
我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...
- hdu 1427 速算24点
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...
- 24点游戏&&速算24点(dfs)
24点游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- hdu1427之速算24点
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- python实现算24的算法
1.介绍 给定4个整数,数字范围在1-13之间,任意使用 + - * / ( ) ,构造出一个表达式,使得最终结果为24,这就是常见的算24的游戏.本文介绍用Python语言实现的两种方式.2.实现思 ...
- python 穷举法 算24点(史上最简短代码)
本来想用回溯法实现 算24点.题目都拟好了,就是<python 回溯法 子集树模板 系列 -- 7.24点>.无奈想了一天,没有头绪.只好改用暴力穷举法. 思路说明 根据四个数,三个运算符 ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
随机推荐
- Struts表单格局;theme三个属性值:simple,xhtml,css_xhtml
转自:http://www.educity.cn/wenda/7156.html 解决Struts2 Form表单自己布局之前先看看 theme 属性, theme属性提供 三个属性值:simple, ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HttpURL
* 步骤: 1. new一个URL对象 2. new一个HttpURLConnection对象 3. connection连接 4. getResponseCode() ...
- JAVA项目JDK版本修改
1.添加JDK window-----> preferences 2.设置默认JDK版本 3.在项目上右键------>Properties
- 暴力+降复杂度 BestCoder Round #39 1002 Mutiple
题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...
- 2015ACM/ICPC亚洲区长春站 E hdu 5531 Rebuild
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- it's hard to say
Ew,it's hard to begin.In fact I don't know what to say either.So here is a sad story.First of all ,m ...
- HDU 4405 期望DP
期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...
- web farm 讨论引出
关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows iis无敌 玩nginx就不要用win系统,必须l ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...