[java]代码库
import java.util.Stack;
/**
* 计算24点
*
* @author LiuFengQingYin
*/
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() : "无解");
}
}