华为机试扑克牌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;
    }
}

#华为#
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
评论
点赞
3
分享
牛客网
牛客企业服务