网易雷火测开笔试题 题解

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

相关推荐

2024-12-27 23:45
已编辑
三江学院 Java
程序员牛肉:死局。学历+无实习+项目比较简单一点。基本就代表失业了。 尤其是项目,功能点实在是太假了。而且提问点也很少。第一个项目中的使用jwt和threadlocal也可以作为亮点写出来嘛?第二个项目中的“后端使用restful风格”,“前端采用vue.JS”,“使用redis”也可以作为亮点嘛? 项目实在是太简单了,基本就是1+1=2的水平。而你目标投递的肯定也是小厂,可小厂哪里有什么培养制度,由于成本的问题,人家更希望你来能直接干活,所以你投小厂也很难投。基本就是死局,也不一定非要走后端这条路。可以再学一学后端之后走测试或者前端。 除此之外,不要相信任何付费改简历的。你这份简历没有改的必要了,先沉淀沉淀
点赞 评论 收藏
分享
评论
3
16
分享

创作者周榜

更多
牛客网
牛客企业服务