From 7a4b7d9be9b7a5c2644569f33c6b20a0b072a1fc Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 31 Oct 2022 23:00:23 +0530 Subject: [PATCH] update --- C++/Program - 28/program-28.cpp | 36 +++++++++++++++++++++++++++++++++ C++/Program - 28/readme.md | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 C++/Program - 28/program-28.cpp create mode 100644 C++/Program - 28/readme.md diff --git a/C++/Program - 28/program-28.cpp b/C++/Program - 28/program-28.cpp new file mode 100644 index 00000000..87c720cd --- /dev/null +++ b/C++/Program - 28/program-28.cpp @@ -0,0 +1,36 @@ +// C++ program to find number of bins required using +// next fit algorithm. +#include +using namespace std; + +// Returns number of bins required using next fit +// online algorithm +int nextFit(int weight[], int n, int c) +{ + // Initialize result (Count of bins) and remaining + // capacity in current bin. + int res = 0, bin_rem = c; + + // Place items one by one + for (int i = 0; i < n; i++) { + // If this item can't fit in current bin + if (weight[i] > bin_rem) { + res++; // Use a new bin + bin_rem = c - weight[i]; + } + else + bin_rem -= weight[i]; + } + return res; +} + +// Driver program +int main() +{ + int weight[] = { 2, 5, 4, 7, 1, 3, 8 }; + int c = 10; + int n = sizeof(weight) / sizeof(weight[0]); + cout << "Number of bins required in Next Fit : " + << nextFit(weight, n, c); + return 0; +} \ No newline at end of file diff --git a/C++/Program - 28/readme.md b/C++/Program - 28/readme.md new file mode 100644 index 00000000..517d02f5 --- /dev/null +++ b/C++/Program - 28/readme.md @@ -0,0 +1,7 @@ +## Given n items of different weights and bins each of capacity c, assign each item to a bin such that number of total used bins is minimized. It may be assumed that all items have weights smaller than bin capacity. + +### Example: Input: weight[] = {4, 8, 1, 4, 2, 1} Bin Capacity c = 10 Output: 2 We need minimum 2 bins to accommodate all items First bin contains {4, 4, 2} and second bin {8, 1, 1} + +### Input: weight[] = {9, 8, 2, 2, 5, 4} Bin Capacity c = 10 Output: 4 We need minimum 4 bins to accommodate all items. + +### Input: weight[] = {2, 5, 4, 7, 1, 3, 8}; Bin Capacity c = 10 Output: 3 \ No newline at end of file