编辑代码

struct Item {
    int weight;
    int value;
};

int maxProfit = 0;

void branchAndBound(int capacity, int currentProfit, int level, struct Item items[], int n) {
    if (level == n) {
        if (currentProfit > maxProfit) {
            maxProfit = currentProfit;
        }
        return;
    }
    
    branchAndBound(capacity, currentProfit, level + 1, items, n);
    
    if (capacity >= items[level].weight) {
        branchAndBound(capacity - items[level].weight, currentProfit + items[level].value, level + 1, items, n);
    }
}

int main() {
    struct Item items[] = {{10, 60}, {20, 100}, {30, 120}};
    int capacity = 50;
    int n = sizeof(items) / sizeof(items[0]);
    
    branchAndBound(capacity, 0, 0, items, n);
    
    printf("最大总价值为: %d\n", maxProfit);
    
    return 0;
}