JAVA实现二十四点_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA实现二十四点

JAVA实现二十四点

 2011/12/28 17:55:40  ITMAOO  http://itmaoo.iteye.com  我要评论(0)
  • 摘要:二十四点:任意给出四个数字,通过加减乘除四则运算得出值为24的算法有多少种。我实现了一个二十四点的算法为对数字和运算符分别两次全排列。详细代码如下:packagemyMath.ershisidian;importitmao.Iershidian;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importjava.util.Set;/***ITMAOO*2011.12
  • 标签:实现 Java
二十四点:任意给出四个数字,通过加减乘除四则运算得出值为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);
}
}

}
   
}


}

}
上一篇: MyEclipse打jar包 下一篇: JS跨域
发表评论
用户名: 匿名