
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Fitting Shelves Problem in C++
In this problem, we are given three integer values W, n, m denoting the length of wall W, size of shelves n, and m. Our task is To Create a Program to solve the Fitting Shelves Problem.
We need to find a way to fit shelves in such a way that the space left after fitting shelves is minimized. A secondary constrain while solving is the cost of making, the larger shelves are more cost-effective so, we need to give them a priority.
The output should be in the following form,
Number of n size shelves number of m size shelves space left
Let’s take an example to understand the problem
Input: W = 12, n = 5, m = 3 Output: 0 4 0
Explanation
Here, we will can fit exactly 4, 3 sized shelves in the wall.
This will make the total length = 4*3 = 12
So, no length in the wall is remaining after fitting.
Solution Approach
A simple solution to the problem is by using the brute force approach which is by checking for each possible combination of fitting shelves in the wall and finding the ones which minimize or eliminate the space length in the wall. For the secondary task, we will be starting with fitting the larger-length shelf fist, this will give the larger one a priority. We will see which combination gives the minimum result with the maximum possible large shelves for the optimum solution.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; void solveFittingShelves(int wall, int m, int n){ int numM = 0, numN = 0, minSpaceLeft = wall; int p = wall/m, q = 0, rem = wall%m; numM = p; numN = q; minSpaceLeft = rem; while (wall >= n) { q += 1; wall = wall - n; p = wall / m; rem = wall % m; if (rem <= minSpaceLeft) { numM = p; numN = q; minSpaceLeft = rem; } } cout<<numM<<" "<<numN<<" "<<minSpaceLeft<<endl; } int main(){ int W = 29, m = 3, n = 9; cout<<"Length of wall : "<<W<<endl; cout<<"Length of shelves : "<<m<<"\t"<<n<<endl; cout<<"Optimal Shelves fitting : "; solveFittingShelves(W, m, n); return 0; }
Output
Length of wall : 29 Length of shelves : 3 9 Optimal Shelves fitting : 0 3 2
- Related Articles
- Sentence Screen Fitting in C++
- Filling Bookcase Shelves in Python
- Curve Fitting Models in Software Engineering
- Partition Problem in C++
- Regression Analysis and the Best Fitting Line using C++
- Finding the smallest fitting number in JavaScript
- Friends Pairing Problem in C++
- Find if neat arrangement of cups and shelves can be made in C++
- Corrupt stack problem in C, C++ program
- 0-1 Knapsack Problem in C?
- Water and Jug Problem in C++
- Minimum Word Break Problem in C++
- 2-Satisfiability(2-SAT) Problem in C/C++?
- A Peterson Graph Problem in C Program?
- Activity Selection Problem (Greedy Algo-1) in C++?
