Pay an amount using the fewest notes and coins of 1000, 500, 200, 100, 50, 20, 10, 5, 2 and 1 taka. Always use as many of the largest value as possible first.
What to build
A program that pays an amount by taking as many of each value as it can, starting with the largest.
Requirements
- Handle the values 1000, 500, 200, 100, 50, 20, 10, 5, 2 and 1 in that order.
- Print one line for every value, even when the count is 0.
Input
One whole number from 0 to 100000.
Output
Ten lines, from 1000 down to 1, each in the form value: count.
Constraints
The amount is from 0 to 100000.
Example
Input:
1270
Output:
1000: 1
500: 0
200: 1
100: 0
50: 1
20: 1
10: 0
5: 0
2: 0
1: 0
Why it works
This set of notes has the property that taking the largest possible value first always gives the fewest notes. That is not true of every set of values.
Examples
1270
1000: 1 500: 0 200: 1 100: 0 50: 1 20: 1 10: 0 5: 0 2: 0 1: 0
8
1000: 0 500: 0 200: 0 100: 0 50: 0 20: 0 10: 0 5: 1 2: 1 1: 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
count = amount // value; then amount = amount % value.
Hint 2
f"{value}: {count}" prints one line in the right form.
Solution
Show one correct solution (try it yourself first)
amount = int(input())
for value in [1000, 500, 200, 100, 50, 20, 10, 5, 2, 1]:
print(f"{value}: {amount // value}")
amount = amount % value