#include <stdio.h>
int max(int a, int b) {
return (a > b) ? a : b;
}
int page(int W, int wt[], int val[], int n) {
int i, w;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
int totalValue = K[n][W];
int remainingWeight = W;
printf("装果\n");
for (i = n; i > 0 && totalValue > 0; i--) {
if (totalValue != K[i - 1][remainingWeight]) {
if (i == 1) {
printf("苹果 15kg 300元\n");
} else if (i == 2) {
printf("香蕉 18kg 180元\n");
} else if (i == 3) {
printf("橘子 10kg 150元\n");
} else if (i == 4) {
printf("猕猴桃 9kg 270元\n");
}
totalValue -= val[i - 1];
remainingWeight -= wt[i - 1];
}
}
return K[n][W];
}
int main() {
int val[] = {300, 180, 150, 270};
int kg[] = {15, 18, 10, 9};
int W = 20;
int n = sizeof(val) / sizeof(val[0]);
printf("最大总价值为: %d\n", page(W, kg, val, n));
return 0;
}