四张牌,通过四则运算得到24,一个数限用一次,快者为胜。
代码:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * 快算24 * @author heyang * */ public class Caculator{ private double anticipate;// 预期结果 private double op1;// 操作数一 private double op2;// 操作数二 private List<String> fomulaList;// 两个操作数进行四则运算能得到预期结果的算式列表 // 仅算24的构造函数 public Caculator(double[] arr){ this(24,arr); } // 完全构造函数,可以指定结果 public Caculator(double anticipate,double[] arr){ // 2 if(arr.length==2){ this.anticipate=anticipate; op1=arr[0]; op2=arr[1]; fomulaList=getFomulaList(); } if(arr.length==3){ this.anticipate=anticipate;// C double[] arrNew;// A double anticipateNew;// arr[2]=B List<String> list; Caculator c; fomulaList=new ArrayList<String>(); /// 0,1,2 // A+B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=anticipate-arr[2]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[2]); } } // A-B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=anticipate+arr[2]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[2]); } } // B-A=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=arr[2]-anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[2]+"-("+str+")"); } } // A*B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=anticipate/arr[2]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[2]); } } // A/B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=anticipate*arr[2]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[2]); } } // B/A=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; anticipateNew=arr[2]/anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[2]+"/("+str+")"); } } /// 1,2,0 // A+B=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=anticipate-arr[0]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[0]); } } // A-B=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=anticipate+arr[0]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[0]); } } // B-A=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=arr[0]-anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[0]+"-("+str+")"); } } // A*B=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=anticipate/arr[0]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[0]); } } // A/B=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=anticipate*arr[0]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[0]); } } // B/A=C arrNew=new double[2]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; anticipateNew=arr[0]/anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[0]+"/("+str+")"); } } /// 0,2,1 // A+B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=anticipate-arr[1]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[1]); } } // A-B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=anticipate+arr[1]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[1]); } } // B-A=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=arr[1]-anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[1]+"-("+str+")"); } } // A*B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=anticipate/arr[1]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[1]); } } // A/B=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=anticipate*arr[1]; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[1]); } } // B/A=C arrNew=new double[2]; arrNew[0]=arr[0]; arrNew[1]=arr[2]; anticipateNew=arr[1]/anticipate; c=new Caculator(anticipateNew,arrNew); list=c.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[1]+"/("+str+")"); } } } if(arr.length==4){ this.anticipate=anticipate;// C double[] arrNew;// A double anticipateNew;// arr[2]=B List<String> list; Caculator cac; fomulaList=new ArrayList<String>(); /// 0,1,2 &3 // A+B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=anticipate-arr[3]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[3]); } } // A-B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=anticipate+arr[3]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[3]); } } // B-A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=arr[3]-anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[3]+"-("+str+")"); } } // A*B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=anticipate/arr[3]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[3]); } } // A/B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=anticipate*arr[3]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[3]); } } // B/A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[2]; anticipateNew=arr[3]/anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[3]+"/("+str+")"); } } /// 0,1,3 & 2 // A+B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=anticipate-arr[2]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[2]); } } // A-B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=anticipate+arr[2]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[2]); } } // B-A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=arr[2]-anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[2]+"-("+str+")"); } } // A*B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=anticipate/arr[2]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[2]); } } // A/B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=anticipate*arr[2]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[2]); } } // B/A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[1]; arrNew[2]=arr[3]; anticipateNew=arr[2]/anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[2]+"/("+str+")"); } } /// 0,3,2&1 // A+B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=anticipate-arr[1]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[1]); } } // A-B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=anticipate+arr[1]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[1]); } } // B-A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=arr[1]-anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[1]+"-("+str+")"); } } // A*B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=anticipate/arr[1]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[1]); } } // A/B=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=anticipate*arr[1]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[1]); } } // B/A=C arrNew=new double[3]; arrNew[0]=arr[0]; arrNew[1]=arr[3]; arrNew[2]=arr[2]; anticipateNew=arr[1]/anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[1]+"/("+str+")"); } } /// 1,2,3&0 // A+B=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=anticipate-arr[0]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")+"+arr[0]); } } // A-B=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=anticipate+arr[0]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")-"+arr[0]); } } // B-A=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=arr[0]-anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[0]+"-("+str+")"); } } // A*B=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=anticipate/arr[0]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")*"+arr[0]); } } // A/B=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=anticipate*arr[0]; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add("("+str+")/"+arr[0]); } } // B/A=C arrNew=new double[3]; arrNew[0]=arr[1]; arrNew[1]=arr[2]; arrNew[2]=arr[3]; anticipateNew=arr[0]/anticipate; cac=new Caculator(anticipateNew,arrNew); list=cac.fomulaList; if(list.size()>0){ for(String str:list){ fomulaList.add(arr[0]+"/("+str+")"); } } // 两个两组的 if(isEqual(arr[0]*arr[1]+arr[2]*arr[3],anticipate)){ fomulaList.add(arr[0]+"*"+arr[1]+"+"+arr[2]+"*"+arr[3]); } if(isEqual(arr[0]*arr[1]-arr[2]*arr[3],anticipate)){ fomulaList.add(arr[0]+"*"+arr[1]+"-"+arr[2]+"*"+arr[3]); } if(isEqual(arr[2]*arr[3]-arr[0]*arr[1],anticipate)){ fomulaList.add(arr[2]+"*"+arr[3]+"-"+arr[0]+"*"+arr[1]); } if(isEqual(arr[0]/arr[1]+arr[2]/arr[3],anticipate)){ fomulaList.add(arr[0]+"/"+arr[1]+"+"+arr[2]+"/"+arr[3]); } if(isEqual(arr[0]/arr[1]-arr[2]/arr[3],anticipate)){ fomulaList.add(arr[0]+"/"+arr[1]+"-"+arr[2]+"/"+arr[3]); } if(isEqual(arr[2]/arr[3]-arr[0]/arr[1],anticipate)){ fomulaList.add(arr[2]+"/"+arr[3]+"-"+arr[0]+"/"+arr[1]); } if(isEqual(arr[0]/arr[2]+arr[1]/arr[3],anticipate)){ fomulaList.add(arr[0]+"/"+arr[2]+"+"+arr[1]+"/"+arr[3]); } if(isEqual(arr[0]/arr[2]-arr[1]/arr[3],anticipate)){ fomulaList.add(arr[0]+"/"+arr[2]+"-"+arr[1]+"/"+arr[3]); } if(isEqual(arr[1]/arr[3]-arr[0]/arr[2],anticipate)){ fomulaList.add(arr[1]+"/"+arr[3]+"-"+arr[0]+"/"+arr[2]); } if(isEqual(arr[0]/arr[3]+arr[1]/arr[2],anticipate)){ fomulaList.add(arr[0]+"/"+arr[3]+"+"+arr[1]+"/"+arr[2]); } if(isEqual(arr[0]/arr[3]-arr[1]/arr[2],anticipate)){ fomulaList.add(arr[0]+"/"+arr[3]+"-"+arr[1]+"/"+arr[2]); } if(isEqual(arr[1]/arr[2]-arr[0]/arr[3],anticipate)){ fomulaList.add(arr[1]+"/"+arr[2]+"-"+arr[0]+"/"+arr[3]); } } } public void printFomula(){ if(fomulaList!=null && fomulaList.size()>0){ Set<String> set=new HashSet<String>(); set.addAll(fomulaList); for(String str:set){ System.out.println(str+"="+anticipate); } }else{ // System.out.println("无解"); // } } // 判断两个数是否相等 private static boolean isEqual(double d1,double d2){ return Math.abs(d1-d2)<0.0001; } /** * 两个数进行四则运算的六种结果 * @return */ private List<String> getFomulaList(){ List<String> retval=new ArrayList<String>(); if(isEqual(op1-op2,anticipate)){ retval.add(op1+"-"+op2); } if(isEqual(op2-op1,anticipate)){ retval.add(op2+"-"+op1); } if(isEqual(op1+op2,anticipate)){ retval.add(op1+"+"+op2); } if(isEqual(op1*op2,anticipate)){ retval.add(op1+"*"+op2); } if(isEqual(op1/op2,anticipate)){ retval.add(op1+"/"+op2); } if(isEqual(op2/op1,anticipate)){ retval.add(op2+"/"+op1); } return retval; } public static void main(String[] args){ double[] arr1={3,6,9,12}; Caculator c1=new Caculator(24,arr1); c1.printFomula(); System.out.println("-----------"); List<double[]> list=new ArrayList<double[]>(); list.add(new double[]{}); list.add(new double[] { 8, 3, 3, 8 }); list.add(new double[] { 3, 7, 3, 7 }); list.add(new double[] { 12, 12, 12, 10 }); list.add(new double[] { 10, 10, 4, 4 }); list.add(new double[] { 1, 4, 5, 6 }); list.add(new double[] { 2, 2, 2, 9 }); list.add(new double[] { 3, 4, 7, 8 }); list.add(new double[] { 11, 11, 1, 5 }); list.add(new double[] { 1, 2, 7, 7 }); list.add(new double[] { 7, 7, 4, 4 }); list.add(new double[] { 1, 4, 5, 6 }); list.add(new double[] { 1, 3, 4, 6 }); list.add(new double[] { 5, 5, 5, 1 }); for(double[] arr:list){ Caculator c=new Caculator(arr); c.printFomula(); } } }
输出:
6.0/((9.0/12.0)/3.0)=24.0 12.0/((9.0/6.0)/3.0)=24.0 ((3.0*12.0)/9.0)*6.0=24.0 (12.0/(9.0/6.0))*3.0=24.0 (12.0/(9.0/3.0))*6.0=24.0 12.0-(3.0-(6.0+9.0))=24.0 ((12.0-3.0)+6.0)+9.0=24.0 ((3.0+9.0)*12.0)/6.0=24.0 ((3.0*12.0)*6.0)/9.0=24.0 ((3.0/9.0)*12.0)*6.0=24.0 (6.0/(9.0/3.0))*12.0=24.0 ((3.0*6.0)/9.0)*12.0=24.0 9.0-(3.0-(6.0+12.0))=24.0 ((12.0-3.0)+9.0)+6.0=24.0 ((6.0/9.0)*12.0)*3.0=24.0 ((9.0-3.0)*6.0)-12.0=24.0 ((9.0-3.0)+6.0)+12.0=24.0 12.0-((3.0-9.0)-6.0)=24.0 ((3.0/9.0)*6.0)*12.0=24.0 ((12.0+9.0)-3.0)+6.0=24.0 6.0-(3.0-(12.0+9.0))=24.0 ((3.0+9.0)/6.0)*12.0=24.0 (9.0-(3.0-12.0))+6.0=24.0 ((3.0*6.0)*12.0)/9.0=24.0 ((6.0-3.0)+12.0)+9.0=24.0 12.0/(6.0/(3.0+9.0))=24.0 ((6.0-3.0)+9.0)+12.0=24.0 ((12.0/9.0)*6.0)*3.0=24.0 12.0-((3.0-6.0)-9.0)=24.0 6.0-((3.0-9.0)-12.0)=24.0 9.0-((3.0-12.0)-6.0)=24.0 ((6.0/9.0)*3.0)*12.0=24.0 ((9.0+12.0)+6.0)-3.0=24.0 3.0/((9.0/12.0)/6.0)=24.0 (6.0-(3.0-12.0))+9.0=24.0 ((12.0/9.0)*3.0)*6.0=24.0 (3.0/(9.0/12.0))*6.0=24.0 ((9.0-3.0)+12.0)+6.0=24.0 (3.0/(9.0/6.0))*12.0=24.0 ((6.0*12.0)*3.0)/9.0=24.0 12.0/(9.0/(3.0*6.0))=24.0 (6.0-(3.0-9.0))+12.0=24.0 ((6.0+12.0)+9.0)-3.0=24.0 ((6.0+9.0)+12.0)-3.0=24.0 12.0/((9.0/3.0)/6.0)=24.0 6.0/(9.0/(3.0*12.0))=24.0 ((6.0*12.0)/9.0)*3.0=24.0 6.0-((3.0-12.0)-9.0)=24.0 (9.0-(3.0-6.0))+12.0=24.0 3.0/(9.0/(6.0*12.0))=24.0 9.0-((3.0-6.0)-12.0)=24.0 ((6.0+12.0)-3.0)+9.0=24.0 (12.0-(3.0-9.0))+6.0=24.0 6.0/((9.0/3.0)/12.0)=24.0 (6.0/(9.0/12.0))*3.0=24.0 (12.0-(3.0-6.0))+9.0=24.0 ((6.0+9.0)-3.0)+12.0=24.0 3.0/((9.0/6.0)/12.0)=24.0 ----------- 8.0/(3.0-(8.0/3.0))=24.0 ((3.0/7.0)+3.0)*7.0=24.0 12.0*12.0-12.0*10.0=24.0 ((10.0*10.0)-4.0)/4.0=24.0 6.0/((5.0/4.0)-1.0)=24.0 4.0/(1.0-(5.0/6.0))=24.0 ((2.0+9.0)*2.0)+2.0=24.0 ((9.0+2.0)*2.0)+2.0=24.0 8.0-((3.0-7.0)*4.0)=24.0 ((7.0-3.0)*4.0)+8.0=24.0 ((11.0*11.0)-1.0)/5.0=24.0 ((7.0*7.0)-1.0)/2.0=24.0 (4.0-(4.0/7.0))*7.0=24.0 6.0/((5.0/4.0)-1.0)=24.0 4.0/(1.0-(5.0/6.0))=24.0 6.0/(1.0-(3.0/4.0))=24.0 (5.0-(1.0/5.0))*5.0=24.0