zoukankan      html  css  js  c++  java
  • 【Kata Daily 190906】Vasya

    题目:

    The new "Avengers" movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single10050 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.

    Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

    Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

    Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.

    -----------------------------------------------------------------------------------------------------------

    找零钱的题目,客人中会出现25,50,100面值的钞票,电影票25块钱。问一条队中,店员能不能卖完电影票而且不用另外找零钱。

    解题办法:

    看下网友的解题思路:

    def tickets(people):
        n25, n50, n100 = 0, 0, 0
        for i in people:
            if i == 25:
                n25 += 1
            elif i == 50:
                n25 -= 1
                n50 += 1
            elif i == 100 and n50>0:
                n25 -= 1
                n50 -= 1
            elif i == 100 and n50==0:
                n25 -= 3
            if n25<0 or n50<0:
                return "NO"
        else:
            return "YES"

    解读:利用钱包中钱的个数来判断是否能够找得开零钱。并对100元的情况进行了分别处理,即优先使用50元来找零。

    还有一种没有通过测试的算法:

    def tickets(people):
        sum, change,a = 0, 0, ""
        for i in people:
            sum += 25
            change = i - 25
            sum -= change
            if sum < 0:
                a = "NO"
            else:
                a = "YES"
        return a

    疑惑:举不出例子来说明该算法的错误之处。另外此段算法给到你,你该如何编写测试用例?

  • 相关阅读:
    Mybatis如何插入空字段
    为什么要将action实例设置为多例
    hibernate dynamic-update="true"属性不起作用原因(转载)
    查找到匹配的进程并关闭 linux ps -ef
    Mac 下解决修改IntelliJ IDEA 由于修改配置之后无法启动问题
    再聊移动端页面的适配
    重学前端
    前端面试
    使用Flexible实现手淘H5页面的终端适配
    vue-cli3.0 使用px2rem 或 postcss-plugin-px2rem
  • 原文地址:https://www.cnblogs.com/bcaixl/p/11474856.html
Copyright © 2011-2022 走看看