Make Solution Continuous

This commit is contained in:
Samuel Oberhofer 2022-06-26 15:32:16 +02:00
parent c9d5a7a84c
commit 8a95eb668b
1 changed files with 11 additions and 0 deletions

View File

@ -1,4 +1,6 @@
#include "backpack.h"
#include <algorithm>
#include <cmath>
/* Optimized insertion sort algorithm utilizing the Item's getRatio() function
*/
@ -50,6 +52,15 @@ void Backpack::greedyPack() {
<< this->availableItems[i].weight << "\t"
<< this->availableItems[i].value
<< "\t Factor: 1.00\n"; // output
} else {
float factor = (float)(this->maxWeight - this->currentWeight) /
this->availableItems[i].weight;
factor = std::round(factor * 100) / 100;
std::cout << "\t" << this->availableItems[i].name << ":\t"
<< this->availableItems[i].weight << "\t"
<< this->availableItems[i].value << "\t Factor: " << factor
<< std::endl;
break;
}
if ((this->currentWeight - this->maxWeight) ==
0) { // if the backpack is completely full, the loop can be exited -