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))

[UCSD白板题] Changing Money的更多相关文章

  1. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  2. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  5. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  6. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  7. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  8. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

  9. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

随机推荐

  1. 转 Visual C++ 将整合Clang

    原文见:http://www.solidot.org/story?sid=45898 微软在11月释出的Visual C++更新将整合Clang开源C和C++编译器,开发者将可以用Clang编译Win ...

  2. wpf 常见死锁方式

    Thread tr0 = new Thread(new ParameterizedThreadStart((obj1) => { lock (aaa) { Thread.Sleep(); thi ...

  3. shll 变量

    name=zhagnsan age=11 echo $ name $age 赋值号两边没有任何空格.当想取shell变量的值时,需要在变量名前加上$字符,当所赋的值中间含有空格时,要加上引号 函数: ...

  4. PLSQL win7 64位

    1. 解压instantclient-basic-win32-11.2.0.1.0.zip至Oracle安装目录的Product下 具体目录如下D:\Oracle\product\instantcli ...

  5. poj 3692 Kindergarten (最大独立集)

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4903   Accepted: 2387 Desc ...

  6. iOS代码实现九宫格

    #define ScreenW [UIScreen mainScreen].bounds.size.width #define ScreenH [UIScreen mainScreen].bounds ...

  7. 空MVC项目找不到System.Web.Optimization的处理办法

    install-package Microsoft.AspNet.Web.Optimization Create the bundle in Global.asax Application_Start ...

  8. Java中四种引用:强、软、弱、虚引用

    这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...

  9. tomcat(一)--java基础

    什么是java java所涉及到的相关概念如下图.总体来说就是java语言.java API.jvm等构成. jvm:java虚拟机,java的代码都是运行在jvm上,这是java语言跨平台的保证,针 ...

  10. PHP正则表达式 /i, /is, /s, /isU等

    PHP正则表达式 /i, /is, /s, /isU等 都是些什么东西呢? i 匹配大小写 s 模式中的圆点元字符(.)匹配所有的字符,包括换行符 x 模式中的空白字符除了被转义的或在字符类中的以外完 ...