题解 | #24点运算#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] strs = scan.nextLine().split(" ");
HashMap<String, Integer> StringToInteger = new HashMap<>();
HashMap<Integer, String> IntegerToString = new HashMap<>();
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 2; i <= 10; i++) {
StringToInteger.put(String.valueOf(i), i);
IntegerToString.put(i, String.valueOf(i));
}
StringToInteger.put("A", 1);
StringToInteger.put("J", 11);
StringToInteger.put("Q", 12);
StringToInteger.put("K", 13);
IntegerToString.put(1, "A");
IntegerToString.put(11, "J");
IntegerToString.put(12, "Q");
IntegerToString.put(13, "K");
for (String str : strs) {
if ("joker".equals(str) || "JOKER".equals(str)) {
System.out.println("ERROR");
return;
}
nums.add(StringToInteger.get(str));
}
for (int num : nums) {
ArrayList<Integer> copyArr = new ArrayList<>(nums);
copyArr.remove(Integer.valueOf(num));
// String tmp = process(String.valueOf(IntegerToString.get(num)), String.valueOf(num), copyArr, IntegerToString);
String tmp = process(String.valueOf(IntegerToString.get(num)), num, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
System.out.println(tmp);
return;
}
}
System.out.println("NONE");
}
/**********************************************************************************/
public static String process(String previousStr, int previousTotal, ArrayList<Integer> nums, HashMap<Integer, String> IntegerToString) {
if (nums.size() == 0) {
return previousTotal == 24 ? previousStr : "NONE";
}
for (int i = 0; i < nums.size(); i++) {
int current = nums.get(i);
ArrayList<Integer> copyArr = new ArrayList<>(nums);
copyArr.remove(Integer.valueOf(current));
String tmp = process(previousStr + "+" + IntegerToString.get(current), previousTotal + current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "-" + IntegerToString.get(current), previousTotal - current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "*" + IntegerToString.get(current), previousTotal * current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "/" + IntegerToString.get(current), previousTotal / current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
}
return "NONE";
}
/**********************************************************************************/
/*
public static String process(String previousStr, String numsStr, ArrayList<Integer> nums, HashMap<Integer, String> IntegerToString) {
if (nums.size() == 0) {
return eval(numsStr) == 24 ? previousStr : "NONE";
}
for (int i = 0; i < nums.size(); i++) {
int current = nums.get(i);
ArrayList<Integer> copyArr = new ArrayList<>(nums);
copyArr.remove(Integer.valueOf(current));
String tmp = process(previousStr + "+" + IntegerToString.get(current), numsStr + "+" + current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "-" + IntegerToString.get(current), numsStr + "-" + current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "*" + IntegerToString.get(current), numsStr + "*" + current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
tmp = process(previousStr + "/" + IntegerToString.get(current), numsStr + "/" + current, copyArr, IntegerToString);
if (!tmp.equals("NONE")) {
return tmp;
}
}
return "NONE";
}
public static int eval(String str) {
String[] strs = str.split("");
LinkedList<String> ll = new LinkedList<>();
for (String tmp : strs) {
if ("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp)) {
ll.add(tmp);
} else {
if (!ll.isEmpty() && !"+".equals(ll.peekLast()) && !"-".equals(ll.peekLast()) && !"*".equals(ll.peekLast()) && !"/".equals(ll.peekLast())) {
String last = ll.peekLast();
ll.removeLast();
ll.add(last + tmp);
} else {
ll.add(tmp);
}
}
}
Stack<String> stack = new Stack<>();
LinkedList<String> suffix = new LinkedList<>();
while (!ll.isEmpty()) {
String tmp = ll.poll();
if (!("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp))) {
suffix.add(tmp);
} else {
if ("*".equals(tmp) || "/".equals(tmp)) {
while (!stack.isEmpty() && !(stack.peek().equals("+") || stack.peek().equals("-"))) {
suffix.add(stack.pop());
}
stack.push(tmp);
} else {
while (!stack.isEmpty()) {
suffix.add(stack.pop());
}
stack.push(tmp);
}
}
}
while (!stack.isEmpty()) {
suffix.add(stack.pop());
}
int ans = 0;
while (!suffix.isEmpty()) {
String tmp = suffix.poll();
if ("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp)) {
int num1 = Integer.valueOf(stack.pop());
int num2 = Integer.valueOf(stack.pop());
if ("+".equals(tmp)) {
ans = num2 + num1;
} else if ("-".equals(tmp)) {
ans = num2 - num1;
} else if ("*".equals(tmp)) {
ans = num2 * num1;
} else {
ans = num2 / num1;
}
stack.push(String.valueOf(ans));
} else {
stack.push(tmp);
}
}
return Integer.valueOf(stack.pop());
}
*/
}