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

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

  • 相关阅读:
    GridView里的数据转化为datatable进行数据传输
    asp.net给asp:button同时添加服务器事件和JS事件
    .msi文件安装出现2503、2502错误
    初识IO流——创建目录和文件
    WinRAR注册
    Ubuntu14.04.1 阿里apt源
    Pycharm远程调试
    Couldn't create temporary file to work with
    Ubuntu14中supervisor的安装及配置
    Ubuntu14.04安装pip及配置
  • 原文地址:https://www.cnblogs.com/bcaixl/p/11474856.html
Copyright © 2011-2022 走看看