网易雷火测开笔试题 题解

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        ArrayList<Integer> arrayList2 = new ArrayList<>();
        String[] s = scanner.nextLine().split(" ");
        for (String s1 : s) {
            String[] split = s1.split(",");
            arrayList1.add(Integer.parseInt(split[0]));
            arrayList2.add(Integer.parseInt(split[1]));
        }
        getMax(arrayList1, arrayList2, 0, 0, 0);

        for (int i = 0; i < res.size(); i++) {
            if (i < res.size() - 1) {
                System.out.print(res.get(i) + " ");
            } else {
                System.out.print(res.get(i));
            }
        }
    }

    public static int sum = Integer.MIN_VALUE;
    public static int W = 20;
    public static ArrayList<Integer> tmp = new ArrayList<>();
    public static ArrayList<Integer> res;

    public static void getMax(ArrayList<Integer> arrayList1, ArrayList<Integer> arrayList2, int i, int num1, int num2) {

        int n = arrayList1.size();
        if (num1 > W) {
            return;
        }
        if (i >= n) {
            if (num2 > sum) {
                sum = num2;
                res = new ArrayList<>();
                for (Integer integer : tmp) {
                    res.add(integer);
                }
            }
            return;
        }
        for (int j = 0; j <= 1; j++) {
            if (j == 0) {
                tmp.add(0);
                getMax(arrayList1, arrayList2, i + 1, num1, num2);
                tmp.remove(tmp.size() - 1);
            } else if (j == 1) {
                tmp.add(1);
                getMax(arrayList1, arrayList2, i + 1, num1 + arrayList1.get(i), num2 + arrayList2.get(i));
                tmp.remove(tmp.size() - 1);
            }
        }
    }

#网易雷火##网易雷火2023秋招笔试虐我的瞬间##笔试##网易雷火游戏测试#
全部评论
主函数不用多说,直接nextLine然后split 对数据处理就用回溯,中间集合要处理好
1 回复
分享
发布于 2022-08-14 18:07
01背包保存路径应该也能过
点赞 回复
分享
发布于 2022-08-14 20:02
联想
校招火热招聘中
官网直投

相关推荐

3 13 评论
分享
牛客网
牛客企业服务