1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.HashSet; 4 import java.util.Scanner; 5 6 import javax.script.ScriptEngine; 7 import javax.script.ScriptEngineManager; 8 import javax.script.ScriptException; 9 10 public class Main { 11 public static void main(String[] args) throws ScriptException { 12 Scanner sc = new Scanner(System.in); 13 while(sc.hasNext()) { 14 String in = sc.nextLine(); 15 //in = "j k q a"; 16 in = in.trim(); 17 String out = game(in); 18 System.out.println(out); 19 } 20 } 21 22 private static String game(String in) throws ScriptException { 23 in = in.toUpperCase(); 24 String[] ins = in.split("[\\s]+"); 25 if(ins.length != 4) {return "ERROR";} 26 for (int i = 0; i < ins.length; i++) { 27 if(ins[i].equals("A")) {ins[i] = "1";} 28 if(ins[i].equals("J")) {ins[i] = "11";} 29 if(ins[i].equals("Q")) {ins[i] = "12";} 30 if(ins[i].equals("K")) {ins[i] = "13";} 31 if(ins[i].equals("JOKER")) {return "ERROR";} 32 } 33 System.out.println(Arrays.asList(ins)); 34 35 //四张牌排序 36 String[] numloc = {"1234","1243","1423","1432","1324","1342", 37 "2134","2143","2314","2341","2413","2431", 38 "3124","3142","3412","3421","3241","3214", 39 "4123","4132","4213","4231","4321","4312"}; 40 int loc; 41 String[] co = {"+","-","*","/"}; 42 ArrayList<String> list = new ArrayList<>(); 43 String[][] str = null; 44 for (int nl = 0; nl < numloc.length; nl++) { 45 str = new String[4][4]; 46 for (int j = 0; j < 3; j++) { 47 loc = Integer.parseInt(""+numloc[nl].charAt(j))-1; 48 for (int j2 = 0; j2 < 4; j2++) { 49 str[j][j2] = ins[loc]+co[j2]; 50 } 51 } 52 for (int j = 0; j < 4; j++) { 53 loc = Integer.parseInt(""+numloc[nl].charAt(3))-1; 54 str[3][j] = ins[loc]; 55 } 56 57 // 58 String a = ""; 59 for (int j1 = 0; j1 < 4; j1++) { 60 for (int j2 = 0; j2 < 4; j2++) { 61 for (int j3 = 0; j3 < 4; j3++) { 62 for (int j4 = 0; j4 < 4; j4++) { 63 a=a+str[0][j1]+str[1][j2]+str[2][j3]+str[3][j4]; 64 list.add(a); 65 a=""; 66 } 67 } 68 } 69 } 70 } 71 72 //加括号,共计扩容6倍 73 list = new ArrayList<>(new HashSet(list)); 74 ArrayList<String> list2 = new ArrayList<>(); 75 for (int i = 0; i < list.size(); i++) { 76 String s = list.get(i); 77 //记录符号位置 78 int[] a = new int[3]; 79 int count = 0; 80 for (int j = 0; j < s.length(); j++) { 81 if(s.charAt(j)==‘+‘||s.charAt(j)==‘-‘||s.charAt(j)==‘*‘||s.charAt(j)==‘/‘) 82 {a[count++] = j;} 83 } 84 //System.out.println(s); 85 String ss ; 86 //第一种 87 ss = "("+s.substring(0,a[1])+")"+s.substring(a[1], s.length()); 88 list2.add(ss); 89 //第二种 90 ss = "("+s.substring(0,a[2])+")"+s.substring(a[2], s.length()); 91 list2.add(ss); 92 //第三种 93 ss = s.substring(0,a[0]+1)+"("+s.substring(a[0]+1, a[2])+")"+s.substring(a[2],s.length()); 94 list2.add(ss); 95 //第四种 96 ss = s.substring(0,a[0]+1)+"("+s.substring(a[0]+1, s.length())+")"; 97 list2.add(ss); 98 //第五种 99 ss = s.substring(0,a[1]+1)+"("+s.substring(a[1]+1, s.length())+")"; 100 list2.add(ss); 101 //第六种,双括号 102 ss = "("+s.substring(0,a[1])+")"+s.substring(a[1],a[1]+1)+"("+s.substring(a[1]+1, s.length())+")"; 103 list2.add(ss); 104 105 //System.out.println(list2); 106 //System.out.println(""); 107 108 } 109 list = new ArrayList<>(new HashSet(list)); 110 list.addAll(list2); 111 System.out.println(list); 112 //System.out.println(list.size()); 113 for (int i = 0; i < list.size(); i++) { 114 115 Object d = null; 116 ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript"); 117 d = (Object) se.eval(list.get(i)); 118 //System.out.println(i+" "+d); 119 double result = 24D; 120 Double re = Double.valueOf(""+d); 121 if ( re>(result-0.0001) && re<(result+0.0001) ) {System.out.println("个数:"+list.size()+" "+list2.size());return "结果:"+list.get(i); } 122 123 } 124 System.out.println("个数:"+list.size()+" "+list2.size()); 125 return "NONE"; 126 } 127 }
自己手写的24点程序(包含括号操作,可适应所有四张牌类型,输入用空格隔开,回车结束)
原文:https://www.cnblogs.com/RYSBlog/p/12853043.html