#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[20];
int weight;
int value;
} Item;
int Bag(int count, int capacity, Item* items) {
int maxWeight = 0;
int maxValue = 0;
int maxCombination = 0;
int i , j ;
for (i = 0; i < (1 << count); ++i) {
int currentWeight = 0;
int currentValue = 0;
for (j = 0; j < count; ++j) {
if ((i >> j) & 1) {
currentWeight += items[j].weight;
currentValue += items[j].value;
}
}
if (currentWeight <= capacity && currentValue > maxValue) {
maxWeight = currentWeight;
maxValue = currentValue;
maxCombination = i;
}
}
printf("菜篮子:");
for (i = 0; i < count; ++i) {
if ((maxCombination >> i) & 1) {
printf("%s ", items[i].name);
}
}
printf("\n");
return maxValue;
}
int main() {
int count = 5;
int capacity = 10;
Item items[5];
strcpy(items[0].name, "苹果");
items[0].weight = 2;
items[0].value = 3;
strcpy(items[1].name, "香梨");
items[1].weight = 3;
items[1].value = 4;
strcpy(items[2].name, "橘子");
items[2].weight = 4;
items[2].value = 5;
strcpy(items[3].name, "西瓜");
items[3].weight = 5;
items[3].value = 8;
strcpy(items[4].name, "草莓");
items[4].weight = 9;
items[4].value = 10;
int maxValue = Bag(count, capacity, items);
printf("一共:%d元\n", maxValue);
return 0;
}