PartialWeight and partialValue for continuous case

This commit is contained in:
Samuel Oberhofer 2022-06-26 15:44:14 +02:00
parent 7dc503ca13
commit cf6edcdff6
2 changed files with 7 additions and 3 deletions

View File

@ -35,6 +35,8 @@ void Backpack::greedyPack(bool continuous) {
this->sortAvailableItems(); // sort the available items first!
const char *text = "";
float partialValue = 0.0f;
float partialWeight = 0.0f;
if (!continuous) {
text = "non-";
}
@ -64,6 +66,8 @@ void Backpack::greedyPack(bool continuous) {
<< this->availableItems[i].weight << "\t"
<< this->availableItems[i].value << "\t Factor: " << factor
<< std::endl;
partialWeight = this->availableItems[i].weight * factor;
partialValue = this->availableItems[i].value * factor;
break;
}
if ((this->currentWeight - this->maxWeight) ==
@ -73,8 +77,8 @@ void Backpack::greedyPack(bool continuous) {
}
}
std::cout << "---------------------------------------------\n"; // output
std::cout << "\tTotal:\t" << this->currentWeight << "\t"
<< this->getValuePacked() << "\n"; // output
std::cout << "\tTotal:\t" << this->currentWeight + partialWeight << "\t"
<< this->getValuePacked() + partialValue << "\n"; // output
}
void Backpack::printItems() {

View File

@ -8,7 +8,7 @@ int main() {
{"4", 8, 5}, {"5", 10, 10}, {"6", 11, 5},
{"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20},
{"10", 30, 20}};
Backpack bp((float)20,
Backpack bp((float)24,
items); // creation of the backpack, utilizing the constructor
bp.greedyPack(false); // greedily pack Backpack non-continuously
bp.greedyPack(true); // greedily pack Backpack continuously