formatting source-code for 153fb7b8a5

This commit is contained in:
github-actions
2020-05-30 04:02:09 +00:00
parent 92fe9495ec
commit 8a2de9842b
175 changed files with 1671 additions and 3460 deletions

View File

@@ -1,25 +1,21 @@
#include <iostream>
using namespace std;
struct Item
{
struct Item {
int weight;
int profit;
};
float profitPerUnit(Item x) { return (float)x.profit / (float)x.weight; }
int partition(Item arr[], int low, int high)
{
int partition(Item arr[], int low, int high) {
Item pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j < high; j++)
{
for (int j = low; j < high; j++) {
// If current element is smaller than or
// equal to pivot
if (profitPerUnit(arr[j]) <= profitPerUnit(pivot))
{
if (profitPerUnit(arr[j]) <= profitPerUnit(pivot)) {
i++; // increment index of smaller element
Item temp = arr[i];
arr[i] = arr[j];
@@ -32,10 +28,8 @@ int partition(Item arr[], int low, int high)
return (i + 1);
}
void quickSort(Item arr[], int low, int high)
{
if (low < high)
{
void quickSort(Item arr[], int low, int high) {
if (low < high) {
int p = partition(arr, low, high);
quickSort(arr, low, p - 1);
@@ -43,8 +37,7 @@ void quickSort(Item arr[], int low, int high)
}
}
int main()
{
int main() {
cout << "\nEnter the capacity of the knapsack : ";
float capacity;
cin >> capacity;
@@ -52,8 +45,7 @@ int main()
int n;
cin >> n;
Item itemArray[n];
for (int i = 0; i < n; i++)
{
for (int i = 0; i < n; i++) {
cout << "\nEnter the weight and profit of item " << i + 1 << " : ";
cin >> itemArray[i].weight;
cin >> itemArray[i].profit;
@@ -65,17 +57,13 @@ int main()
float maxProfit = 0;
int i = n;
while (capacity > 0 && --i >= 0)
{
if (capacity >= itemArray[i].weight)
{
while (capacity > 0 && --i >= 0) {
if (capacity >= itemArray[i].weight) {
maxProfit += itemArray[i].profit;
capacity -= itemArray[i].weight;
cout << "\n\t" << itemArray[i].weight << "\t"
<< itemArray[i].profit;
}
else
{
} else {
maxProfit += profitPerUnit(itemArray[i]) * capacity;
cout << "\n\t" << capacity << "\t"
<< profitPerUnit(itemArray[i]) * capacity;