# 编程之美1.1624点游戏解法一java版

2.输出表达式的地方还没想出怎么解决

/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test;

import java.math.BigDecimal;
import static java.math.BigDecimal.ROUND_HALF_DOWN;
import java.util.*;

/**
*
*/
public class PointsGame {

static double[] number = {9, 9, 6, 2};
static String[] result =new String[4];

public static void main(String[] args) {
for(int i =0;i<4;i++){
result[i] ="";
}
boolean flag = pointsGame(4);
System.out.println("flag="+flag);
for(int i =0;i<4;i++){
System.out.println(result[i]);
}
}

private static boolean pointsGame(int n) {
if (n < 2) {
if (number[0] == 24) {
return true;
} else {
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
String expa,expb;
a = number[i];
b = number[j];
number[j]=number[n-1];

expa=result[i];
expb=result[j];
result[j]=result[n-1];

result[i]="("+expa+"+"+expb+")";
number[i] = a+b;
if(pointsGame(n-1))
return true;

result[i]="("+expa+"-"+expb+")";
number[i]=a-b;
if(pointsGame(n-1))
return true;

result[i]="("+expb+"-"+expa+")";
number[i]=b-a;
if(pointsGame(n-1))
return true;

result[i]="("+expa+"*"+expb+")";
number[i]=a*b;
if(pointsGame(n-1))
return true;

if(b!=0){
result[i]="("+expa+"/"+expb+")";
number[i]=a/b;
if(pointsGame(n-1))
return true;
}
if(a!=0){
result[i]="("+expb+"/"+expa+")";
number[i]=b/a;
if(pointsGame(n-1))
return true;
}
number[i]=a;
number[j]=b;
result[i]=expa;
result[j]=expb;

}
}
return false;
}

}
运行结果：true (((+)-)*)

• 本文已收录于以下专栏：

举报原因： 您举报文章：编程之美1.1624点游戏解法一java版 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)