01背包问题 --贪心(性价比)

eebd6190320a1faee902fda44e16792a.png
adc9091c4685dc5f0f07ba425c8297e7.png

参考视频https://www.bilibili.com/video/av36136952?from=search&seid=10516072371338376794

递归解法

import java.util.*;
public class problem {
    public int max(int a, int b) {
        if (a > b) {
            return a;
        } else
            return b;
    }
    public int B(int[][] a, int x, int k) {                                     // x代表前面还有x件东西可以拿,k表示还有k个空间可以存放东西
        if (x < 0) {                                                                      // 递归的出口
            return 0;
        } else if (k < a[x][1]) {                                                    // 递归的内部条件
            return B(a, x - 1, k);
        } else {
            return max(B(a, x - 1, k - a[x][1]) + a[x][0], B(a, x - 1, k));
        }
    }
    public static void main(String[] args) {
        problem p = new problem();
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();                                                 // 代表有几种商品可以拿0 1 2 3 4 5 6 7排列
        int m = input.nextInt();                                                // 代表背包可以装多少体积
        int[][] a = new int[n][2];
        for (int i = 0; i < n; i++) {
            a[i][0] = input.nextInt();                                             // 价格
            a[i][1] = input.nextInt();                                             // 体积
        }
        n = n - 1;                                                                    // 使排列从0 1 2 3 4 5 6 7开始
        System.out.println(p.max(p.B(a, n - 1, m - a[n][1]) + a[n][0], p.B(a, n - 1, m)));
    }
}

非递归解法

import java.util.*;
public class problem {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();// 代表有几种商品可以拿 0 1 2 3 4 5 6 7排列
        int m = input.nextInt();// 代表背包可以装多少体积
        int[] v = new int[n];// 0到n-1
        int[] w = new int[n];// 从第0个商品开始 0到n-1
        // 下面为每个商品的价格
        for (int i = 0; i < n; i++) {
            v[i] = input.nextInt();// 0到n-1价格
        }
        // 下面为每个商品的体积
        for (int i = 0; i < n; i++) {
            w[i] = input.nextInt();// 0到n-1体积
        }
        // 背包问题非递归的话,要把所有结果放在一个二维数组中
        int[][] B = new int[n + 1][m + 1];// 空间能达到(0~n,0~m)
        // 二维数组的第0行以及第0列都为0;第一值开始的话为(1,1)
        int x, y;// x竖种类 y横体积
        for (x = 1; x < n + 1; x++) {
            for (y = 1; y < m + 1; y++) {
                if (w[x - 1] > y) {
                    B[x][y] = B[x - 1][y];
                } else {
                    int value1 = B[x - 1][y - w[x - 1]] + v[x - 1];
                    int value2 = B[x - 1][y];
                    if (value1 > value2) {
                        B[x][y] = value1;
                    } else
                        B[x][y] = value2;
                }
            }
        }
        System.out.println(B[n][m]);
    }
}

转载于:https://www.cnblogs.com/cznczai/p/11148190.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值