c++
You have tons of toys that you want to divide into boxes. Each box can store at most T toys and you got plenty of boxes. To distribute the toys, your plan is to divide the N toys in half, forming two smaller piles, then continue dividing each of the small piles in half until we get piles that can fit in the boxes. Find how many boxes you will need.
This strategy may not give you the least number of boxes. note that when you divide an odd number pile in half, one of them will have one more than the other.
Input Format
Two lines with integers N and C
Constraints
1 < C < N < 10,000
Output Format
Numbers of boxes you end up using with the given strategy
Example test cases: but they are not all the test cases.
Input :11 3
Output
4
Explanation
First, you divide 11 to 5 and 6. They are still too big. Split 5 -> 2, 3 Split 6 -> 3, 3 Each of them can now fit in the given boxes, so we need four.
Input
8 1
output: 8
/*
* Complete the 'findBoxes' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER count
* 2. INTEGER capacity
*/
int findBoxes(int count, int capacity) {
}
Please help me with this. Thank you.
#include <iostream>
using namespace std;
int findBoxes(int count, int capacity) {
if (capacity >= count)
return 1;
if (count % 2 == 0)
return 0 + findBoxes (count/2,capacity) + findBoxes
(count/2,capacity);
if (count % 2 != 0)
return 0 + findBoxes (count/2,capacity) + findBoxes (count/2 +
1,capacity);
}
int main()
{
cout << findBoxes (11,3) << endl;
cout << findBoxes (8,1) << endl;
}
Get Answers For Free
Most questions answered within 1 hours.