PartialWeight and partialValue for continuous case
This commit is contained in:
parent
7dc503ca13
commit
cf6edcdff6
|
|
@ -35,6 +35,8 @@ void Backpack::greedyPack(bool continuous) {
|
||||||
|
|
||||||
this->sortAvailableItems(); // sort the available items first!
|
this->sortAvailableItems(); // sort the available items first!
|
||||||
const char *text = "";
|
const char *text = "";
|
||||||
|
float partialValue = 0.0f;
|
||||||
|
float partialWeight = 0.0f;
|
||||||
if (!continuous) {
|
if (!continuous) {
|
||||||
text = "non-";
|
text = "non-";
|
||||||
}
|
}
|
||||||
|
|
@ -64,6 +66,8 @@ void Backpack::greedyPack(bool continuous) {
|
||||||
<< this->availableItems[i].weight << "\t"
|
<< this->availableItems[i].weight << "\t"
|
||||||
<< this->availableItems[i].value << "\t Factor: " << factor
|
<< this->availableItems[i].value << "\t Factor: " << factor
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
partialWeight = this->availableItems[i].weight * factor;
|
||||||
|
partialValue = this->availableItems[i].value * factor;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((this->currentWeight - this->maxWeight) ==
|
if ((this->currentWeight - this->maxWeight) ==
|
||||||
|
|
@ -73,8 +77,8 @@ void Backpack::greedyPack(bool continuous) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "---------------------------------------------\n"; // output
|
std::cout << "---------------------------------------------\n"; // output
|
||||||
std::cout << "\tTotal:\t" << this->currentWeight << "\t"
|
std::cout << "\tTotal:\t" << this->currentWeight + partialWeight << "\t"
|
||||||
<< this->getValuePacked() << "\n"; // output
|
<< this->getValuePacked() + partialValue << "\n"; // output
|
||||||
}
|
}
|
||||||
|
|
||||||
void Backpack::printItems() {
|
void Backpack::printItems() {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ int main() {
|
||||||
{"4", 8, 5}, {"5", 10, 10}, {"6", 11, 5},
|
{"4", 8, 5}, {"5", 10, 10}, {"6", 11, 5},
|
||||||
{"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20},
|
{"7", 12, 10}, {"8", 15, 17}, {"9", 15, 20},
|
||||||
{"10", 30, 20}};
|
{"10", 30, 20}};
|
||||||
Backpack bp((float)20,
|
Backpack bp((float)24,
|
||||||
items); // creation of the backpack, utilizing the constructor
|
items); // creation of the backpack, utilizing the constructor
|
||||||
bp.greedyPack(false); // greedily pack Backpack non-continuously
|
bp.greedyPack(false); // greedily pack Backpack non-continuously
|
||||||
bp.greedyPack(true); // greedily pack Backpack continuously
|
bp.greedyPack(true); // greedily pack Backpack continuously
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue