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 ...
随机推荐
- hdu 1215 筛法
求小于n的n的因子之和 Sample Input 3 2 10 20Sample Output 1 8 22 #include<cstdio> #include<iostream&g ...
- C#中使用ListView动态添加数据不闪烁并显示当前插入值
首先,自定义一个类ListViewNF,继承自 System.Windows.Forms.ListView class ListViewNF : System.Windows.Forms.ListVi ...
- web应用配置
tomcat 的 server.html 配置文件 加在</Host>之上 <Context path=”/itcast” docBase=”c:\news” /> path虚 ...
- 小明A+B[HDU2096]
小明A+B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Js作用域与作用域链详解
一直对Js的作用域有点迷糊,今天偶然读到Javascript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作用 ...
- The Storage Situation: Removable Storage
http://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html There is a lot of co ...
- CentOS 下安装无线哥的老爷机DELL的无线驱动
使用命令检测网卡 lspci | grep Network 为“0c:00.0 Network controller: Broadcom Corporation BCM4312 802.11b/g ...
- Marching Cube
GPU-Marching-Cubes An Implementation of the Marching Cubes[1] Algorithm Marching Cubes Matlab The St ...
- php页面之间传值
echo("<script>window.open('2.php?head=".$head."');<script>");
- 紧挨导航栏下的链接a标签失效
在我编完网页测试的的时候,发现导航下的链接不能用,不出现小手的图表,而下面的相同的链接都能用.如下图所示 遇到的相同问题的另一个未完成的页面如图 先不谈论其原因,说一下我对第一个页面的解决办法,我在导 ...