二十四点

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Scanner;



public class Count24 {
    private List<String> answerList=new ArrayList<String>();
    public List<String> getAnswerList() {
        return answerList;
    }
    public static class Data{
        public float[]  arr;
        public String expStr="";
        public String[] strs;
        public Data(){}
        public Data(int a,int b,int c,int d) {
            arr=new float[]{a,b,c,d};
            strs=new String[]{a+"",b+"",c+"",d+""};
            expStr=a+"";
        }
        public Data(int arr[]) {
            this.arr=new float[]{arr[0],arr[1],arr[2],arr[3]};
            this.strs=new String[]{arr[0]+"",arr[1]+"",arr[2]+"",arr[3]+""};
        }
    }
    public void count(Data data){
        float[] arr=data.arr;
        if(arr.length<=1){
            if(arr.length==1&&arr[0]==24){
                answerList.add(data.expStr.substring(1, data.expStr.length()-1));
            }
            return ;
        }
        for(int i=0,len=arr.length;i<len-1; i++){
            for(int j=i+1;j<len;j++){
                float x=arr[i];
                float y=arr[j];
                String xs=data.strs[i];
                String ys=data.strs[j];
                for(int k=0;k<6;k++){
                    Data newData=getNewArr(data,i);
                    switch(k){
                        case 0:
                        newData.arr[j-1]=x+y;
                            newData.expStr=xs+"+"+ys;
                        break;
                        case 1:
                        newData.arr[j-1]=x-y;
                            newData.expStr=xs+"-"+ys;
                        break;
                        case 2:
                        newData.arr[j-1]=y-x;
                        newData.expStr=ys+"-"+xs;
                        break;
                        case 3:
                        newData.arr[j-1]=x*y;
                            newData.expStr=xs+"*"+ys;
                        break;
                        case 4:
                        if(y!=0){
                            newData.arr[j-1]=x/y;
                                newData.expStr=xs+"/"+ys;
                        }else {
                            continue;
                        }
                        break;
                        case 5:
                        if(x!=0){
                            newData.arr[j-1]=y/x;
                                newData.expStr=ys+"/"+xs;
                        }else {
                            continue;
                        }
                        break;
                    }
                    newData.expStr="("+newData.expStr+")";
                    newData.strs[j-1]=newData.expStr;
                    count(newData);
                }
            }
        }
        
    }
    private static Data getNewArr(Data data, int i) {
        Data newData=new Data();
        newData.expStr=data.expStr;
        newData.arr=new float[data.arr.length-1];
        newData.strs=new String[data.arr.length-1];
        for(int m=0,len=data.arr.length,n=0;m<len;m++){
            if(m!=i){
                newData.arr[n]=data.arr[m];
                newData.strs[n]=data.strs[m];
                n++;
            }
        }
        return newData;
    }
    
    public static final List<String> easyCount(int[] curRandNums){
        Count24 count24=new Count24();
        count24.count(new Data(curRandNums));
        Set<String> set=new HashSet<String>(count24.getAnswerList());
        return new ArrayList<String>(set);
    }
    
    public static void main(String[] args) throws InterruptedException {
        long start=System.currentTimeMillis();
        Scanner in=new Scanner(System.in);
        System.out.println("Please input four counts:");
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int d=in.nextInt();
        if(a>0 && b>0 && c>0 && d>0 && a<14 && b<14 && c<14 && d<14  ){
        	List<String> answerStris=easyCount(new int[]{a,b,c,d});
        	for (String string : answerStris) {
        		System.out.println(string);
        	}
        }
        else System.out.println("Error!");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值