题解 | #24点运算#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
本人菜鸡方法比较笨主要是用递归,一上来将输入的转为String数组方便递归,并判断是否有大小王,若有大小王直接输出ERROR并结束,若无大小王先对四张牌递归排序获得所有排列方式,再对排序过的四张牌递归插入运算符,并获得所有排列方式,并放入集合中。最后对集合遍历并计算结果,若结果为24输出结果并结束遍历
import java.util.*;
public class Main{
static List<String> anssList = new ArrayList<String>(); public static void main(String[] args){ Scanner sc = new Scanner(System.in); List<String> list = new ArrayList<String>(); while(sc.hasNext()){ list.add(sc.next()); } boolean status = true; String[] str = new String[list.size()]; for (int i = 0; i < str.length; i++) { if(list.get(i).equals("joker") || list.get(i).equals("JOKER")){ System.out.println("ERROR"); status = false; } str[i] = list.get(i); } if(status){ Integer[] i = new Integer[list.size()]; boolean r = false; String[] chars = new String[]{"+", "-", "*", "/"}; List<String[]> intList = new ArrayList<String[]>(); getInts(str, 0, new String[i.length], intList); intList.forEach(k -> { getAns(k, 0 ,chars , ""); }); int temp = 0; if(anssList.size() != 0){ for (int i1 = 0; i1 < anssList.size(); i1++) { if(getSum(anssList.get(i1)) == 24){ System.out.println(anssList.get(i1)); i1 = anssList.size(); } temp ++; } } if (temp == anssList.size()) System.out.println("NONE"); } } static Integer getNumber(String c){ Integer num = null; c = c.replace(" ", ""); try { switch(c){ case "A": num = 1; break; case "J": num = 11; break; case "Q": num = 12; break; case "K": num = 13; break; default: num = Integer.parseInt(c); break; } } catch (NumberFormatException e) { num = null; } return num; } static Integer getSum(String str){ Integer ans = 0; String[] strs = new String[7]; for (int i = 0; i < strs.length; i++) { strs[i] = str.substring(i, i + 1); } for (int i = 0; i + 2 < strs.length; i += 2) { Integer a = getNumber(strs[i]); Integer b = getNumber(strs[i + 2]); if(a == null || b == null){ return null; } int sum1 = getSum1(a, b, String.valueOf(strs[i + 1])); if(i >= 4){ ans = sum1; } else { strs[i + 2] = sum1 + ""; } } return ans; } static Integer getSum1(Integer a, Integer b, String c){ Integer ans = 0; if(c.equals("+")){ ans = a + b; } if(c.equals("-")){ ans = a - b; } if(c.equals("*")){ ans = a * b; } if(c.equals("/")){ ans = a / b; } return ans; } static void getInts(String[] is, int k, String[] ans, List<String[]> intList){ String[] newAns = new String[ans.length]; for (int i = 0; i < newAns.length; i++) { newAns[i] = ans[i]; } for (int i = 0; i < is.length; i++) { newAns[k] = is[i]; if(1 == is.length){ intList.add(newAns); } else { int temp = k + 1; String[] ints1 = coypInts(is, i); getInts(ints1, temp, newAns, intList); } } } static void getAns(String[] is, int k, String[] chars, String ans){ for(int i = k; i < is.length;){ ans += is[i]; String anss = String.valueOf(ans); if(i == is.length - 1 && anss.length() == 7){ anssList.add(anss); } i++; if(i < is.length){ for (int j = 0; j < chars.length; j++) { String anss1 = String.valueOf(anss); anss1 += chars[j]; getAns(is, i, chars, anss1); } } } } static String[] coypInts(String[] chars, int i){ String[] c = new String[chars.length - 1]; Integer count = 0; for (int i1 = 0; i1 < chars.length; i1++) { if(i1 != i){ c[count++] = chars[i1]; } } return c; } static String[] coypChars(String[] chars, int i){ String[] c = new String[chars.length - 1]; int count = 0; for (int i1 = 0; i1 < chars.length; i1++) { if(i1 != i){ c[count++] = chars[i1]; } } return c; }
}