#include <stdio.h>
#include <stdlib.h>
typedef struct {
int weight;
int value;
} Item;
int compare(const void *a, const void *b) {
Item *item1 = (Item *)a;
Item *item2 = (Item *)b;
double valuePerWeight1 = (double)item1->value / item1->weight;
double valuePerWeight2 = (double)item2->value / item2->weight;
return (valuePerWeight2 > valuePerWeight1) - (valuePerWeight1 > valuePerWeight2);
}
float bound(Item items[], int n, int W, int index, int currWeight, int currValue) {
if (currWeight >= W) return 0;
float maxValue = currValue;
int totWeight = currWeight;
for (int i = index; i < n; i++) {
if (totWeight + items[i].weight <= W) {
totWeight += items[i].weight;
maxValue += items[i].value;
} else {
int remain = W - totWeight;
maxValue += (items[i].value / (float)items[i].weight) * remain;
break;
}
}
return maxValue;
}
int knapsack(Item items[], int n, int W) {
qsort(items, n, sizeof(Item), compare);
int maxValue = 0;
return maxValue;
}
int main() {
Item items[] = {{10, 60}, {20, 100}, {30, 120}};
int W = 50;
int n = sizeof(items) / sizeof(items[0]);
printf("最大价值: %d\n", knapsack(items, n, W));
return 0;
}