二十四点:任意给出四个数字,通过加减乘除四则运算得出值为24的
算法有多少种。我实现了一个二十四点的算法为对数字和运算符分别两次全排列。
详细代码如下:
package myMath.ershisidian;
import itmao.Iershidian;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* ITMAOO
* 2011.12.23
* **/
public class Main {
int count=-1;
double sum=0.00;
Main(int a,int b,int c,int d){
ArrayList array=new ArrayList();
array.add(a);
array.add(b);
array.add(c);
array.add(d);
rankAll(array,new ArrayList());
}
public static void main(String[] args) {
new Main(30,24,8,12);
}
/******
* 全排列算法一,实现数字的全排
* ******/
void rankAll(ArrayList array,ArrayList target){
if(array.size()==0){
Map<Integer,String> outs=new HashMap<Integer,String>();
double[] intArray={Double.valueOf(target.get(0).toString()),
Double.valueOf(target.get(1).toString()),
Double.valueOf(target.get(2).toString()),
Double.valueOf(target.get(3).toString())};
digui(intArray,sum,outs,count);
}
for(int i=0;i < array.size();i++ ){
ArrayList newArray=new ArrayList(array);
ArrayList newtarget=new ArrayList(target);
newtarget.add(newArray.get(i));
newArray.remove(i);
rankAll(newArray,newtarget);
}
}
/******
* 全排列算法二,实现运算符的全排列
* ******/
public void digui(double[] array,double sum,Map outs,int count){
count++;
//处理
加法,并
递归调用
if(count<=3){
outs.put(count,"+");
digui(array,sum+array[count],outs,count);
// sum=sum-array[count];digui(array,sum);
}
//输出
if(count==4&&sum==24.00){
System.out.println(outs.get(0).toString()+(int)array[0]+outs.get(1).toString()+(int)array[1]+outs.get(2).toString()+(int)array[2]+outs.get(3).toString()+(int)array[3]+"=24");
}
//处理
减法,并
递归调用
if(count<=3){
outs.remove(count);
outs.put(count, "-");
digui(array,sum-array[count],outs,count);
}
//处理乘法,并递归调用
if(0<count&&count<=3){
outs.remove(count);
outs.put(count, "*");
if(outs.get(count-1).toString().equals("+")){
digui(array,sum-array[count-1]+array[count]*array[count-1],outs,count);
}else if (outs.get(count-1).toString().equals("-")){
digui(array,sum+array[count-1]-array[count]*array[count-1],outs,count);
}else if (outs.get(count-1).toString().equals("*")){
if(outs.get(count-2).toString().equals("+")){
digui(array,sum-array[count-2]*array[count-1]+array[count-2]*array[count-1]*array[count],outs,count);
}else if (outs.get(count-2).toString().equals("-")){
digui(array,sum+array[count-2]*array[count-1]-array[count-2]*array[count-1]*array[count],outs,count);
}else if (outs.get(count-2).toString().equals("*")){
if(outs.get(count-3).toString().equals("+")){
digui(array,array[count-3]*array[count-2]*array[count-1]*array[count],outs,count);;
}else if (outs.get(count-3).toString().equals("-")){
digui(array,-array[count-3]*array[count-2]*array[count-1]*array[count],outs,count);
}
}
}
}
//处理除法,并递归调用
if(1<count&&count<=3){
outs.remove(count);
outs.put(count, "/");
if(outs.get(count-1).toString().equals("+")){
digui(array,sum-array[count-1] + array[count-1]/array[count],outs,count);
}else if (outs.get(count-1).toString().equals("-")){
digui(array,sum+array[count-1] - array[count-1]/array[count],outs,count);
}else if (outs.get(count-1).toString().equals("*")){
if(outs.get(count-2).toString().equals("+")){
digui(array,sum-array[count-2]*array[count-1]+array[count-2]*array[count-1]/array[count],outs,count);
}else if (outs.get(count-2).toString().equals("-")){
digui(array,sum+array[count-2]*array[count-1]-array[count-2]*array[count-1]/array[count],outs,count);
}else if (outs.get(count-2).toString().equals("*")){
if(outs.get(count-3).toString().equals("+")){
digui(array,array[count-3]*array[count-2]*array[count-1]/array[count],outs,count);;
}else if (outs.get(count-3).toString().equals("-")){
digui(array,-array[count-3]*array[count-2]*array[count-1]/array[count],outs,count);
}
}else if (outs.get(count-2).toString().equals("/")){
if(outs.get(count-3).toString().equals("+")){
digui(array,array[count-3]/array[count-2]*array[count-1]/array[count],outs,count);;
}else if (outs.get(count-3).toString().equals("-")){
digui(array,-array[count-3]/array[count-2]*array[count-1]/array[count],outs,count);
}
}
}else if (outs.get(count-1).toString().equals("/")){
if(outs.get(count-2).toString().equals("+")){
digui(array,sum-array[count-2]/array[count-1]+array[count-2]/array[count-1]/array[count],outs,count);
}else if (outs.get(count-2).toString().equals("-")){
digui(array,sum+array[count-2]/array[count-1]-array[count-2]/array[count-1]/array[count],outs,count);
}else if (outs.get(count-2).toString().equals("*")){
if(outs.get(count-3).toString().equals("+")){
sum=array[count-3]*array[count-2]/array[count-1]/array[count];digui(array,sum,outs,count);;
}else if (outs.get(count-3).toString().equals("-")){
sum=-array[count-3]*array[count-2]/array[count-1]/array[count];digui(array,sum,outs,count);
}
}else if (outs.get(count-2).toString().equals("/")){
if(outs.get(count-3).toString().equals("+")){
sum=array[count-3]/array[count-2]/array[count-1]/array[count];digui(array,sum,outs,count);;
}else if (outs.get(count-3).toString().equals("-")){
sum=-array[count-3]/array[count-2]/array[count-1]/array[count];digui(array,sum,outs,count);
}
}
}
}
}
}