[UCSD白板题] Changing Money

时间:2021-01-29 09:05:43

Problem Introduction

In this problem,you will design an algorithm for changing money optimally.

Problem Description

Task.The goal in this problem is to find the minimum number of coins needed to change the input value(an integer) into coins with denominations 1,5,and 10.

Input Format.The input consists of a single integer \(m\).

Constraints.\(1 \leq m \leq 10^3\).

Output Format.Output the minimum number of coins with denominations 1,5,10 that changes m.

Sample 1.
Input:

2

Output:

2

Explanation:
2=1+1

Sample 2.
Input:

28

Output:

6

Explanation:
28=10+10+5+1+1+1

Solution

# Uses python3
import sys

def get_change(n):
    count = 0
    coins = [10, 5, 1]
    for coin in coins:
        if coin <= n:
            count += n // coin
            n %= coin
    return count

if __name__ == '__main__':
    n = int(sys.stdin.read())
    print(get_change(n))