完全背包
完全背包和0-1背包的区别在于:物品是否可以重复使用
思路:对于完全背包问题,内层循环的遍历方式应该是从weight[i]开始一直遍历到V,而不是从V到weight[i]。这样可以确保每种物品可以被选择多次放入背包,从而求解完全背包问题。
对于完全背包问题,需要对内层循环进行调整,以确保每种物品可以被选择多次放入背包。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 研究材料种类
int V = sc.nextInt(); // 行李箱空间
int[] values = new int[N]; // 物品价值
int[] weight = new int[N]; // 物品重量
// 依次输入每种物品的重量和价值
for (int i = 0; i < N; i++) {
weight[i] = sc.nextInt(); // 物品重量
values[i] = sc