#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点的更多相关文章

  1. 洛谷P1236 算24点

    题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为“算24点”.您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数进行适当的算术运算, ...

  2. 洛谷 P1236 算24点

    题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为"算24点".您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数 ...

  3. 洛谷【P1236】算24点

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...

  4. hdu 1427 速算24点

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...

  5. 24点游戏&&速算24点(dfs)

    24点游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  6. hdu1427之速算24点

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  7. python实现算24的算法

    1.介绍 给定4个整数,数字范围在1-13之间,任意使用 + - * / ( ) ,构造出一个表达式,使得最终结果为24,这就是常见的算24的游戏.本文介绍用Python语言实现的两种方式.2.实现思 ...

  8. python 穷举法 算24点(史上最简短代码)

    本来想用回溯法实现 算24点.题目都拟好了,就是<python 回溯法 子集树模板 系列 -- 7.24点>.无奈想了一天,没有头绪.只好改用暴力穷举法. 思路说明 根据四个数,三个运算符 ...

  9. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

随机推荐

  1. sql 截取字符串与 截取字符串最长的字符串

    ); set @str='aa,32,22,55,7'; ) as '第一个逗号的索引值' )),),),'') as '第一个值' ),len(@str)) as '从第一逗号开始截取出后面的字符串 ...

  2. mac OS X操作--快捷键

  3. 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

    题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...

  4. BZOJ3919 : [Baltic2014]portals

    预处理出每个点上下左右能延伸到的最远点以及到它们的距离的最小值md. 然后spfa,一个点除了可以以1的代价到达四周的点之外,还可以以md+1的代价到达四个方向能到达的最远点. #include< ...

  5. CC150 - 11.3

    Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...

  6. 【BZOJ】3809: Gty的二逼妹子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...

  7. BZOJ4143 [AMPPZ2014]The Lawyer

    Description Byteasar要制订m天的会议计划,一共有n场会议,第i场会议开始于第d[i]天的第a[i]秒,结束于第d[i]天的第b[i]秒. 对于每一天,请找出这一天的两场会议i,j, ...

  8. Json 数组拼接

    var str1 = {"name": "apple", "sex": "21"};                 / ...

  9. NHibernate's inverse - what does it really mean?

    NHibernate's concept of 'inverse' in relationships is probably the most often discussed and misunder ...

  10. 像jQuery那样,采用链式方法,封装一个方法:CSS()

    主要思路就是:返回this对象,将所获取的操作元素放入一个数组中.在原型中添加拓展方法 <html> <head> <title></title> &l ...