为什么和样列中的输出不一样

package Sjfx;

import java.util.Arrays;
import java.util.Scanner;

public class Beibao {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("物品数:");
    int n = sc.nextInt();
    float[] w = new float[n];
    float[] v = new float[n];
    float[] x = new float[n];

    System.out.println("背包容量:");
    float c = sc.nextFloat();

    System.out.println("每个物品重量:");
    for (int i = 0; i < n; i++) {
        w[i] = sc.nextFloat();
    }

    System.out.println("每个物品价值:");
    for (int i = 0; i < n; i++) {
        v[i] = sc.nextFloat();
    }

    float opt = knapsack(c, w, v, x);
    System.out.println("最大价值:" + opt);
}
public static float knapsack(float c, float[] w, float[] v, float[] x) {
    int n = v.length;
    Element[] d = new Element[n]; // 物品数组
    for (int i = 0; i < n; i++) // 数组初始化
        d[i] = new Element(w[i], v[i], i);
    Arrays.sort(d); // 物品按照单位价值排序
    int i;
    float opt = 0; // 当前装入背包的价值
    for (i = 0; i < n; i++)
        x[i] = 0;
    for (i = 0; i < n; i++) { // 扫描物品
        if (d[i].w > c) // d[i].w表示扫描到第i个物品时取出其重量
            break; // 物品i装不下时跳出
        x[d[i].i] = 1; // 物品i装入背包
        opt += d[i].v;
        c -= d[i].w; // 背包当前容量
    }
    if (i < n) { // 背包未装满还有剩余物品时
        x[d[i].i] = c / d[i].w; // 装入部分物品i
        opt += x[d[i].i] * d[i].v;
    }
    System.out.print("此时包中装了");
    for (int j = 0; j < n; j++) {
        if (x[j] != 0) {
            System.out.print("第" + (j + 1) + "件和" + x[j] + "的");
        }
    }
    System.out.println("");
    return opt;
}
public static class Element implements Comparable<Beibao.Element> {
    float v;
    float w;
    int i;


    public Element(float w, float v, int i) {
        this.w = w;
        this.v=v;
        this.i = i;
    }
    @Override
    public int compareTo(Beibao.Element o) {
        if (this.w < o.w)
            return -1;
        else if (this.w == o.w)
            return 0;
        else
            return 1;
    }
}

}

img

img


import java.util.Arrays;
import java.util.Scanner;

public class Beibao {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("物品数:");
            int n = sc.nextInt();
            float[] w = new float[n];
            float[] v = new float[n];
            float[] x = new float[n];

            System.out.println("背包容量:");
            float c = sc.nextFloat();

            System.out.println("每个物品重量:");
            for (int i = 0; i < n; i++) {
                w[i] = sc.nextFloat();
            }

            System.out.println("每个物品价值:");
            for (int i = 0; i < n; i++) {
                v[i] = sc.nextFloat();
            }

            float opt = knapsack(c, w, v, x);
            System.out.println("最大价值:" + opt);
        }
    }

    public static float knapsack(float c, float[] w, float[] v, float[] x) {
        int n = v.length;
        Element[] d = new Element[n]; // 物品数组
        for (int i = 0; i < n; i++) // 数组初始化
            d[i] = new Element(w[i], v[i], i);
        quickSort(d, 0, n - 1); // 物品按照单位价值排序
        int i;
        float opt = 0; // 当前装入背包的价值
        for (i = 0; i < n; i++)
            x[i] = 0;
        for (i = 0; i < n; i++) { // 扫描物品
            if (d[i].w > c) // d[i].w表示扫描到第i个物品时取出其重量
                break; // 物品i装不下时跳出
            x[d[i].i] = 1; // 物品i装入背包
            opt += d[i].v;
            c -= d[i].w; // 背包当前容量
        }
        if (i < n) { // 背包未装满还有剩余物品时
            x[d[i].i] = c / d[i].w; // 装入部分物品i
            opt += x[d[i].i] * d[i].v;
        }
        System.out.print("此时包中装了");
        for (int j = 0; j < n; j++) {
            if (x[j] != 0) {
                System.out.print("第" + (j + 1) + "件的" + x[j] + "和");
            }
        }
        System.out.println("");
        return opt;
    }

    public static class Element {
        float v; // 物品价值
        float w; // 物品重量
        int i; // 物品编号

        public Element(float w, float v, int i) {
            this.w = w;
            this.v = v;
            this.i = i;
        }
    }

    public static void quickSort(Element[] d, int left, int right) {
        if (left < right) {
            int i = left, j = right;
            Element x = d[left];
            while (i < j) {
                while (i < j && d[j].v / d[j].w <= x.v / x.w)
                    j--;
                if (i < j)
                    d[i++] = d[j];
                while (i < j && d[i].v / d[i].w > x.v / x.w)
                    i++;
                if (i < j)
                    d[j--] = d[i];
            }
            d[i] = x;
            quickSort(d, left, i - 1);
            quickSort(d, i + 1, right);
        }
    }
}

样例应该是装入背包中的物品价值最大化,你应该是只要能装就先装入,没考虑价值最大化。你需要把各种可能的情况都列出来,比如物品1能放,但是不放入,留着空间放其它物品,然后计算这些物品的价值。最后在所有可能的组合中取物品价值最大的那个。

package Sjfx;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Element implements Comparable<Element> {
    float weight;

    float value;

    int index;

    Element(float weight, float value, int index) {
        this.weight = weight;
        this.value = value;
        this.index = index;
    }

    @Override
    public int compareTo(Element o) {
        return Float.compare(o.value, value);
    }
}

public class Bag {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int objectCount;
        float bagCapacity;
        float[] weights;
        float[] values;

        System.out.print("物品数: ");
        objectCount = scanner.nextInt();

        System.out.print("背包容量: ");
        bagCapacity = scanner.nextFloat();

        System.out.print("每个物品重量: ");
        weights = new float[objectCount];
        for (int i = 0; i < objectCount; i++) {
            weights[i] = scanner.nextFloat();
        }

        System.out.print("每个物品价值: ");
        values = new float[objectCount];
        for (int i = 0; i < objectCount; i++) {
            values[i] = scanner.nextFloat();
        }

        knapsack(objectCount, bagCapacity, weights, values);
    }

    private static void knapsack(int objCount, float bagCap, float[] weights, float[] values) {
        List<Element> elements = new ArrayList<>();
        for (int i = 0; i < objCount; i++) {
            elements.add(new Element(weights[i], values[i], i));
        }
        elements.sort(Element::compareTo);

        float maxValue = 0;

        List<String> addedIndices = new ArrayList<>();

        float leftBagCap = bagCap;
        int index = 0;
        for (Element elem : elements) {
            if (elem.weight > leftBagCap) break;
            leftBagCap -= elem.weight;
            maxValue += elem.value;
            index++;
            addedIndices.add(Integer.toString(elem.index + 1));
        }

        float lastWeight = 0;
        int lastIndex = 0;
        if (leftBagCap > 0) {
            Element e = elements.get(index);
            maxValue += e.value * (leftBagCap / e.weight);
            lastWeight = e.weight;
            lastIndex = e.index + 1;
        }

        System.out.println("最大价值: " + maxValue);
        System.out.print("此时包中装了第 " + String.join("、", addedIndices) + " ");

        if (leftBagCap > 0) {
            System.out.print("和 " + (int) leftBagCap + "/" + (int) lastWeight + " 的第 " + lastIndex + " ");
        }

        System.out.println("件物品");
    }
}