用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

24点求解(使用加减乘除及括号)

2013-08-26 作者: 流风清音举报

[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() : "无解");
	}

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...