简单算法

简介: python 简单算法
def discount(size, count):
    price_8oz = 1.20
    price_12oz = 1.75
    price_12oz_discount = 1.35
    if size == 8:
        return price_8oz * count
    elif size == 12:
        if count <= 4:
            return price_12oz * count
        elif count > 4:
            # divmod() divides one number by another and returns both the number of times
            #     the second number fits into the first, but also the remainder.
            # Here we calculate how many times five goes into the customer order 
            #     and how many extra cups are left over.
            fivecount, remainder = divmod(count, 5)
            return price_12oz_discount * fivecount * 5 + 1.75 * remainder
# Here we test the code to make sure that it works across several iterations of five counts
#    with 12 ounce cups.
for count in range(2, 12):
    print('count:', count, 'cost:', discount(12, count))
目录
相关文章
|
4月前
|
设计模式 算法 知识图谱
算法设计与分析(贪心法)
【1月更文挑战第1天】在分析问题是否具有最优子结构性质时,通常先设出问题的最优解,给出子问题的解一定是最优的结论。证明思路是:设原问题的最优解导出子问题的解不是最优的,然后在这个假设下可以构造出比原问题的最优解更好的解,从而导致矛盾。(一个问题能够分解成各个子问题来解决,通过各个子问题的最优解能递推到原问题的最优解,此时原问题的最优解一定包含各个子问题的最优解,这是能够采用贪心法来求解问题的关键)贪心选择性质是指所求问题的整体最优解可以通过一系列局部最优的选择获得,即通过一系列的逐步局部最优选择使得最终的选择方案是全局最优的。
74 1
|
算法 程序员
【简单算法】什么是复杂度?
【简单算法】什么是复杂度?
|
算法
【算法设计与分析】3、贪心法
【算法设计与分析】3、贪心法
206 0
|
算法
【算法设计与分析】4、动态规划法
【算法设计与分析】4、动态规划法
309 0
【算法提高——第三讲(二)】图论(2)
【算法提高——第三讲(二)】图论(2)
【算法提高——第三讲(二)】图论(2)
【算法提高——第三讲(二)】图论(1)
【算法提高——第三讲(二)】图论(1)
【算法提高——第三讲(二)】图论(1)
【算法提高——第三讲(一)】图论(2)
【算法提高——第三讲(一)】图论(2)
【算法提高——第三讲(一)】图论(2)
【算法提高——第三讲(二)】图论(4)
【算法提高——第三讲(二)】图论(4)
【算法提高——第三讲(二)】图论(4)
【算法提高——第三讲(二)】图论(3)
【算法提高——第三讲(二)】图论(3)
【算法提高——第三讲(二)】图论(3)