利用Java實現01揹包問題的貪心算法

一、問題描述

給定n種物品和一個揹包。物品i的重量是Wi,其價值爲Vi,揹包的容量爲C。應如何選擇裝入揹包的物品,使得裝入揹包中物品的總價值最大?

二、算法策略

   單位價值化

三、核心代碼

static double Knapsack(double[] a, double[] b, double[] x, double C, int n) {
        sort(a, b, n);
        int i;
        double total = 0;
        for (i = 0; i < n; i++) {
            if (a[i] <= C) {//如果揹包剩餘的容量大於等於第i個物體的重量
                x[i] = 1;   //把第i個物體整個裝進揹包
                C = C - a[i];  //揹包的剩餘容量減少了第i個物體的重量
            } else {
                break;  //退出循環
            }
        }
        if (i < n) {//判斷是否第n個物體整個裝進去揹包裏了,如果i<=n表示否定
            x[i] = C / a[i];
        }
        for (i = 0; i < n; i++) {
            total = total + x[i] * b[i];
        }
        return total;
    }

四、整體代碼


package 第四章;

import java.util.Scanner;

public class beibao {

    public static void main(String args[]) {
        int n, i, j;
        double C;
        System.out.println("請輸入待選擇的物品的個數:");
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        System.out.println("請輸入揹包容量:");
        C = scanner.nextDouble();
        double[] a = new double[n];    //物品重量數組
        double[] b = new double[n];    //物品價值數組
        double[] x = new double[n];
        for (i = 0; i < n; i++) {
            a[i] = Math.floor(Math.random() * 20);
        }
        for (i = 0; i < n; i++) {
            b[i] = Math.floor(Math.random() * 20);
        }
        System.out.println("隨機產生的物品重量爲:");
        for (i = 0; i < n; i++) {
            System.out.print(a[i] + "  ");
        }
        System.out.println();
        System.out.println("隨機產生的物品價值爲:");
        for (i = 0; i < n; i++) {
            System.out.print(b[i] + "  ");
        }
        System.out.println();
        double value = Knapsack(a, b, x, C, n);
        System.out.println("最大價值爲:" + value);
    }

    static void sort(double[] a, double[] b, int n) {
        double[] c = new double[n];
        for (int i = 1; i < n; i++) {
            c[i] = b[i] / a[i];
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n - i; j++) {
                if (c[j] < c[j + 1]) {

                    double temp = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = temp;

                    double temp2 = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp2;

                    double temp3 = b[j];
                    b[j] = b[j + 1];
                    b[j + 1] = temp3;
                }
            }
        }
    }

    static double Knapsack(double[] a, double[] b, double[] x, double C, int n) {
        sort(a, b, n);
        int i;
        double total = 0;
        for (i = 0; i < n; i++) {
            if (a[i] <= C) {//如果揹包剩餘的容量大於等於第i個物體的重量
                x[i] = 1;   //把第i個物體整個裝進揹包
                C = C - a[i];  //揹包的剩餘容量減少了第i個物體的重量
            } else {
                break;  //退出循環
            }
        }
        if (i < n) {//判斷是否第n個物體整個裝進去揹包裏了,如果i<=n表示否定
            x[i] = C / a[i];
        }
        for (i = 0; i < n; i++) {
            total = total + x[i] * b[i];
        }
        return total;
    }
}

五、運行結果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章