龚秋志


私信TA

用户名:uq_48078956563

访问量:48696

签 名:

等  级
排  名 34
经  验 13286
参赛次数 30
文章发表 64
年  龄 19
在职情况 学生
学  校 湖北生物科技职业学院
专  业 计算机应用

  自我简介:

import java.util.Scanner;

public class 简化型背包 {

    static int V, W;
    static int[] p = new int[6];
    static int[] v = new int[6];
    static int[] w = new int[6];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        V = sc.nextInt();// 最大体积
        W = sc.nextInt();// 最大重量
        for (int i = 1; i < 6; i++) {
            p[i] = sc.nextInt();
            v[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
        dfs(0, 0, 0, 0);
        System.out.println(Max);
    }

    static int Max = Integer.MIN_VALUE;

    public static void dfs(int MaxV, int MaxW, int i, int P) {
        MaxV += v[i];
        MaxW += w[i];
        P += p[i];
        Max = Math.max(P, Max);
        if (MaxV == V || MaxW == W)return;
            
        for (int j = i + 1; j < 6; j++) {

            if (MaxV + v[j] <= V && MaxW + w[j] <= W) {
                dfs(MaxV, MaxV, j, P);
            }

        }
    }
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区