华为机试扑克牌24点问题通过率95没有输入输出数据怎么回事

import java.util.*;

//Poker的二十四点运算
public class Main {
    public static Map<String, Integer> map = new HashMap<String, Integer>() {
        {
            put("2", 2);
            put("3", 3);
            put("4", 4);
            put("5", 5);
            put("6", 6);
            put("7", 7);
            put("8", 8);
            put("9", 9);
            put("10", 10);
            put("J", 11);
            put("Q", 12);
            put("K", 13);
            put("A", 1);
        }
    };
    public static Map<Integer, String> map2 = new HashMap<Integer, String>() {
        {
            put(2,"2");
            put(3,"3");
            put(4,"4");
            put(5,"5");
            put(6,"6");
            put(7,"7");
            put(8,"8");
            put(9,"9");
            put(10,"10");
            put(11,"J");
            put(12,"Q");
            put(13,"K");
            put(1,"A");
        }
    };
    public static boolean isFind=false;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            String s=sc.nextLine();
            if(s.contains("joker")||s.contains("JOKER")||s.contains("1")){
                System.out.println("ERROR");
                isFind=true;
            }
            else{
                String[] chars=s.split(" ");
                List<Integer> list=new ArrayList<Integer>();
                for (int i = 0; i < chars.length; i++) {
                    list.add(map.get(chars[i]));
                }
                List<Character> list1=new ArrayList<Character>();
                List<Integer> list2=new ArrayList<Integer>();
                for (int i = 0; i < list.size(); i++) {
                    int value=list.get(i);
                    list.remove(i);
                    list2.add(value);
                    helper(list,value,list1,list2);
                    list2.remove(list2.size()-1);
                    list.add(i,value);
                }
            }
            if(isFind==false){
                    System.out.println("NONE");
                }
            isFind=false;
        }
    }

    private static boolean helper(List<Integer> list, int value,List<Character> list1,List<Integer> list2) {
        if(list.size()==0&&value==24&&isFind==false){
            isFind=true;
            for (int i = 0; i < list1.size(); i++) {
                System.out.print(map2.get(list2.get(i))+list1.get(i));
            }
            System.out.println(map2.get(list2.get(list2.size()-1)));
            return true;
        }
        else if(list.size()==0){return false;}
        else{
            for (int i = 0; i < list.size(); i++) {
                int tmp_value=list.get(i);
                list.remove(i);
                list1.add('+');
                list2.add(tmp_value);
                helper(list, value+tmp_value,list1,list2);
                list1.remove(list1.size()-1);
                list1.add('-');
                helper(list, value-tmp_value,list1,list2);
                list1.remove(list1.size()-1);
                list1.add('*');
                helper(list, value*tmp_value,list1,list2);
                list1.remove(list1.size()-1);
                if(value%tmp_value==0){
                    list1.add('/');
                    helper(list, value*tmp_value,list1,list2);
                    list1.remove(list1.size()-1);
                }
                list2.remove(list2.size()-1);
                list.add(i,tmp_value);
            }
        }
        return false;
    }
}

#华为#
全部评论

相关推荐

昨天 11:23
重庆邮电大学 C++
点赞 评论 收藏
分享
牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
10-11 15:42
皖西学院 Java
青鱼LINK:我硕士,也是java0面试,吾道不孤
点赞 评论 收藏
分享
点赞 3 评论
分享
牛客网
牛客企业服务