代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
24点求解(使用加减乘除及括号)
作者:
LuFang
/ 发布于
2013/7/1
/
530
import java.util.Stack; /** * 计算24点 * * @author fang.lu@yeepay.com */ public class Game24 { private final static double _24 = 24; private final static double A = 1; private final static double J = 11; private final static double Q = 12; private final static double K = 13; private final static double[] seed = { A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K }; private final double[] pokerFaces = new double[4]; private final Stack<Object> partOne = new Stack<Object>(); private final Stack<Object> partTwo = new Stack<Object>(); private final Stack<Object> partThree = new Stack<Object>(); public boolean hasSolution() { pokerFaces[0] = seed[(int) Math.round(Math.random() * 12)]; pokerFaces[1] = seed[(int) Math.round(Math.random() * 12)]; pokerFaces[2] = seed[(int) Math.round(Math.random() * 12)]; pokerFaces[3] = seed[(int) Math.round(Math.random() * 12)]; System.out.println("4张牌为: " + pokerFaces[0] + ", " + pokerFaces[1] + ", " + pokerFaces[2] + ", " + pokerFaces[3]); return (_24 == calculateAll(pokerFaces[0], pokerFaces[1], pokerFaces[2], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[0], pokerFaces[1], pokerFaces[3], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[0], pokerFaces[2], pokerFaces[3], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[0], pokerFaces[2], pokerFaces[1], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[0], pokerFaces[3], pokerFaces[2], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[0], pokerFaces[3], pokerFaces[1], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[0], pokerFaces[2], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[0], pokerFaces[3], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[2], pokerFaces[0], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[2], pokerFaces[3], pokerFaces[0])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[3], pokerFaces[0], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[1], pokerFaces[3], pokerFaces[2], pokerFaces[0])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[0], pokerFaces[1], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[0], pokerFaces[3], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[1], pokerFaces[0], pokerFaces[3])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[1], pokerFaces[3], pokerFaces[0])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[3], pokerFaces[0], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[2], pokerFaces[3], pokerFaces[1], pokerFaces[0])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[0], pokerFaces[2], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[0], pokerFaces[1], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[1], pokerFaces[0], pokerFaces[2])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[1], pokerFaces[2], pokerFaces[0])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[2], pokerFaces[0], pokerFaces[1])) || (_24 == calculateAll(pokerFaces[3], pokerFaces[2], pokerFaces[1], pokerFaces[0])); } private double calculateAll(double i, double l, double m, double n) { if (_24 == i + calculateLast3(_24 - i, l, m, n)) { partOne.push(i); partOne.push('+'); return _24; } if (_24 == i - calculateLast3(i - _24, l, m, n)) { partOne.push(i); partOne.push('-'); return _24; } if (_24 == calculateLast3(i + _24, l, m, n) - i) { partThree.push('-'); partThree.push(i); return _24; } if (_24 == i * calculateLast3(_24 / i, l, m, n)) { partOne.push(i); partOne.push('*'); return _24; } if (_24 == i / calculateLast3(i / _24, l, m, n)) { partOne.push(i); partOne.push('/'); return _24; } if (_24 == calculateLast3(i * _24, l, m, n) / i) { partThree.push('/'); partThree.push(i); return _24; } return Double.NaN; } private double calculateLast3(double i, double l, double m, double n) { if (i == l + calculateLast2(i - l, m, n)) { partTwo.push('('); partTwo.push(l); partTwo.push('+'); partThree.push(')'); return i; } if (i == l - calculateLast2(l - i, m, n)) { partTwo.push('('); partTwo.push(l); partTwo.push('-'); partThree.push(')'); return i; } if (i == calculateLast2(l + i, m, n) - l) { partTwo.push('('); partThree.push('-'); partThree.push(l); partThree.push(')'); return i; } if (i == l * calculateLast2(i / l, m, n)) { partTwo.push(l); partTwo.push('*'); return i; } if (i == l / calculateLast2(l / i, m, n)) { partTwo.push('('); partTwo.push(l); partTwo.push('/'); partThree.push(')'); return i; } if (i == calculateLast2(l / i, m, n) / l) { partTwo.push('('); partThree.push('/'); partThree.push(l); partThree.push(')'); return i; } return Double.NaN; } private double calculateLast2(double d, double m, double n) { if (d == m + n) { partThree.push('('); partThree.push(m); partThree.push('+'); partThree.push(n); partThree.push(')'); return d; } if (d == m - n) { partThree.push('('); partThree.push(m); partThree.push('-'); partThree.push(n); partThree.push(')'); return d; } if (d == m * n) { partThree.push(m); partThree.push('*'); partThree.push(n); return d; } if (d == m / n) { partThree.push('('); partThree.push(m); partThree.push('/'); partThree.push(n); partThree.push(')'); return d; } return Double.NaN; } public String getSolution() { return (partOne.toString() + partTwo.toString() + partThree.toString()) .replace("[", "").replace("]", "").replace(",", ""); } /** * 测试入口 * * @param args */ public static void main(String[] args) { Game24 game = new Game24(); System.out.println(game.hasSolution() == true ? "解为: " + game.getSolution() : "无解"); } }
试试其它关键字
24点
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
LuFang
贡献的其它代码
(
2
)
.
24点求解(使用加减乘除及括号)
.
Java跨平台的精髓
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3