您的位置:首页 > 其它

一个24点的算法

2017-08-28 13:18 393 查看
import java.util.Random;

public class HelloWorld {
public static String formular;

public static void main(String[] args) {
int[] card= new int[4];
//Generate 4 cards randomly
int max=13;
int min=1;
Random random = new Random();
for (int i = 0 ;i<4;i++) {
int s = random.nextInt(max)%(max-min+1) + min;
card[i] = s;
System.out.println("Card " + i + ": " + s);
}

if (canCalculate(card[0],card[1],card[2],card[3],24)) {
System.out.println("formular: " + formular+ "=24");
} else
{
System.out.println("Can't find answer");
}

}

//To check function(a,b,c,d) = result
public static boolean canCalculate(int a, int b, int c, int d, int result) {
int[] input = {a,b,c,d};
for (int i=0; i<4; i++) {
//other input index
int index1 = i+1>3?i-3:i+1;
int index2 = i+2>3?i-2:i+2;
int index3 = i+3>3?i-1:i+3;
//Scenario 1: input[i]+function(other inputs)
if (input[i] <= result) {
if (canCalculate(input[index1], input[index2], input[index3], result-input[i])) {
formular=input[i] + "+(" + formular + ")";
return true;
}
}

//Scenario 2: input[i]-function(other inputs)
if (input[i] > result) {
if (canCalculate(input[index1], input[index2], input[index3], input[i]-result)) {
formular = input[i] + "-(" + formular + ")";
return true;
}
}

//Scenario 3: input[i] * function(other inputs)
if (resul
4000
t%input[i] == 0 ) {
if (canCalculate(input[index1], input[index2], input[index3], result/input[i])) {
formular=input[i] + "*(" + formular + ")";
return true;
}
}

//Scenario 4: input[i] / function(other inputs)
if (input[i]%result == 0 ) {
if (canCalculate(input[index1], input[index2], input[index3], input[i]/result)) {
formular=input[i] + "/(" + formular + ")";
return true;
}
}
}
return false;
}
//To check function(a,b,c) = result
public static boolean canCalculate(int a, int b, int c, int result) {
int[] input = {a,b,c};
for (int i=0; i<3; i++) {
//Scenario 1: input[i]+function(other inputs)
if (input[i] <= result) {
if (canCalculate(input[i+1>2?i-2:i+1], input[i+2>2?i-1:i+2], result-input[i])) {
formular=input[i] + "+(" + formular + ")";
return true;
}
}

//Scenario 2: input[i]-function(other inputs)
if (input[i] > result) {
if (canCalculate(input[i+1>2?i-2:i+1], input[i+2>2?i-1:i+2], input[i]-result)) {
formular = input[i] + "-(" + formular + ")";
return true;
}
}

//Scenario 3: input[i] * function(other inputs)
if (result%input[i] == 0 ) {
if (canCalculate(input[i+1>2?i-2:i+1], input[i+2>2?i-1:i+2], result/input[i])) {
formular=input[i] + "*(" + formular + ")";
return true;
}
}

//Scenario 4: input[i] / function(other inputs)
if (input[i]%result == 0 ) {
if (canCalculate(input[i+1>2?i-2:i+1], input[i+2>2?i-1:i+2], input[i]/result)) {
formular=input[i] + "/(" + formular + ")";
return true;
}
}
}
return false;
}

//To check function(a,b) = result
public static boolean canCalculate(int a, int b, int result) {

if (a+b==result) {
formular = a + "+" + b ;
return true;
}
if (a*b==result)  {
formular = a + "*" + b ;
return true;
}

if (a-b==result) {
formular = a + "-" + b ;
return true;
}

if (a/b==result & a%b == 0) {
formular = a + "/" + b ;
return true;
}
if ((b-a) == result) {
formular = b + "-" + a ;
return true;
}
if (b/a==result & b%a==0) {
formular = b + "/" + a ;
return true;
}
return false;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: