Work out how many boxes are needed to pack some items.
What to build
A program in which rounding up gives the right answer.
Requirements
- Read the number of items and how many fit in one box.
- Print the number of boxes needed, using
math.ceil. - Print how many spaces are left free.
Input
Two lines, each with one whole number.
Output
Two lines: boxes B, then free F.
Constraints
Items are from 0 to 1000000. Capacity is from 1 to 1000.
Example
Input:
10
3
Output:
boxes 4
free 2
Why it works
A part-filled box is still a box, so the division must round up. math.ceil does exactly that.
Examples
Example 1 · Input
10 3
Expected output
boxes 4 free 2
Example 2 · Input
12 4
Expected output
boxes 3 free 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
math.ceil rounds up.
Hint 2
The free space is boxes * capacity - items.
Solution
Show one correct solution (try it yourself first)
import math
items = int(input())
capacity = int(input())
boxes = math.ceil(items / capacity)
print("boxes", boxes)
print("free", boxes * capacity - items)