题解 | #24点运算#
24点运算
http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
//分别求4个数字的排序,操作符的排序
//Ps:操作符可以重复
import java.util.*;
public class Main{
private static HashMap<Character,Integer> map=new HashMap<>();
private static HashMap<Integer,Character> map2=new HashMap<>();
static{
map.put('A', 1);map.put('J', 11);map.put('Q', 12);map.put('K', 13);map.put('-', 10);
for(int i=2;i<=9;i++){
map.put((char)('0'+i), i);
}
}
static{
map2.put(1,'A');map2.put(11,'J');map2.put(12,'Q');map2.put(13,'K');map2.put(10,'-');
for(int i=2;i<=9;i++){
map2.put(i,(char)('0'+i));
}
}
public static void main(String[] args)throws Exception{
Scanner in=new Scanner(System.in);
Solution solution=new Solution();
String s=in.nextLine();
char[] nums=new char[4];
String[] strs=s.split(" ");
for(int i=0;i<4;i++){
if(strs[i].equals("joker")||strs[i].equals("JOKER")){
System.out.println("ERROR");
return;
}
if(strs[i].equals("10")){
nums[i]='-';
}else
nums[i]=strs[i].charAt(0);
}
ArrayList<ArrayList<Character>> res1=new ArrayList<>();
ArrayList<ArrayList<Character>> res2=new ArrayList<>();
solution.perm(nums, new Stack<Character>(), res1);
solution.noperm(new char[]{'+','-','*','/'}, new Stack<Character>(), res2);
for(int i=0;i<res1.size();i++){
Stack<Integer> stack1=new Stack<>();
for(int k=0;k<res1.get(i).size();k++){
stack1.push(map.get(res1.get(i).get(k)));
}
for(int j=0;j<res2.size();j++){
Stack<Character> stack2=new Stack<>();
for(int m=0;m<res2.get(j).size();m++){
stack2.push(res2.get(j).get(m));
}
if(solution.core(stack1, stack2)){
//System.out.println(stack1+" "+stack2);
char a=map2.get(stack1.pop()), b=map2.get(stack1.pop()), c=map2.get(stack1.pop()), d=map2.get(stack1.pop());
char op1=stack2.pop(),op2=stack2.pop(),op3=stack2.pop();
System.out.println(""+a+op1+b+op2+c+op3+d);
return;
}
}
}
System.out.println("NONE");
in.close();
}
}
class Solution{
//求值
public boolean core(Stack<Integer> stack1,Stack<Character> stack2){
//System.out.println(stack1+""+s2);
Stack<Integer> s1=(Stack<Integer>)stack1.clone();
Stack<Character> s2=(Stack<Character>)stack2.clone();
//System.out.print(s1+""+s2+"=");
while(!s2.isEmpty()){
char op=s2.pop();
int temp=0;
int a=s1.pop();
int b=s1.pop();
if(op=='+'){
temp=a+b;
}
if(op=='-'){
temp=a-b;
}
if(op=='*'){
temp=a*b;
}
if(op=='/'){
temp=a/b;
}
s1.push(temp);
}
return s1.pop()==24?true:false;
}
//四个数全排列
public void perm(char[] array,Stack<Character> stack,ArrayList<ArrayList<Character>> res){
if(array.length<=0){
res.add(new ArrayList<>(stack));
}else{
for(int i=0;i<array.length;i++){
char[] temp=new char[array.length-1];
System.arraycopy(array, 0, temp, 0, i);
System.arraycopy(array,i+1, temp, i, array.length-i-1);
stack.push(array[i]);
perm(temp, stack,res);
stack.pop();
}
}
}
//四种操作符
public void noperm(char[] array,Stack<Character> stack,ArrayList<ArrayList<Character>> res){
if(stack.size()==3){
res.add(new ArrayList<>(stack));
}else{
for(int i=0;i<array.length;i++){
stack.push(array[i]);
noperm(array, stack, res);
stack.pop();
}
}
}
}