【Kata Daily 190906】Vasya - Clerk(职员)
题目:
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 single100, 50 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
疑惑:举不出例子来说明该算法的错误之处。另外此段算法给到你,你该如何编写测试用例?
【Kata Daily 190906】Vasya - Clerk(职员)的更多相关文章
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【Kata Daily 191012】Find numbers which are divisible by given number
题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)
题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...
- 【Kata Daily 190923】Odder Than the Rest(找出奇数)
题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...
- 【Kata Daily 190920】Square(n) Sum(平方加总)
题目: Complete the square sum function so that it squares each number passed into it and then sums the ...
- 【Kata Daily 190919】Sort Out The Men From Boys(排序)
题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...
- 【Kata Daily 190918】Spacify(插空)
题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...
随机推荐
- C++ 异常处理 catch(...)介绍
转载:https://blog.csdn.net/fcsfcsfcs/article/details/77717567 catch(-)能够捕获多种数据类型的异常对象,所以它提供给程序员一种对异常 对 ...
- Rolf Dobelli 《清醒思考的艺术》
为了避免输光自己靠勤奋积累的财产,罗尔夫·多贝里列了一份系统性思维错误的清单.这一份清单可以和查理·芒格的<人类误判心理学>对照查看. 自本杰明·富兰克林以来,电闪雷鸣没有减少变弱或响声变 ...
- 使用phpword获取doc中的表格数据
1. 首先确定使用phpword是可以读取word文档中表格里面的数据, 使用的phpword版本0.17.0 2.理解word文档内容的存储逻辑规则(这里只做简单概述) 一般做博文喜欢直接贴代码,直 ...
- GML与KML的区别
1.GML是基于XML的地理信息的传输.存储编码,它包括空间的和非空间的地理特征和地理范畴.GML是空 间数据编码.传输.存储.发布的国际标准KML是一个OGC标准 2.GML专注于地理信息的结构与内 ...
- 第一月多测师讲解_ linux_vim命令_004
一. vi/vim 编辑器共分为三种模式: 命令模式(Command mode),"ESC" 输入模式(Insert mode) 底线命令模式(Last line mode) 命令 ...
- 智能指针(1)-std::unique_ptr
std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...
- JS实现将二维数组生成到页面上
前言 之前没说过数组,现在来写一下数组 CSS span { border:2px solid skyblue; width:30px; height: 30px; display: inline-b ...
- python第三章:函数
在前面章节中,介绍了一些input(),print(),len()等内建函数,还有random,math等标准库相关函数,这些都是可以直接使用的,但是很多时候,我们也是可以编写自己的函数. 看个例子: ...
- 不同系统执行相同shell脚本,出现Syntax error: "(" unexpected错误解决
例如shell脚本在centos系统中能正常执行,而在ubuntu系统中执行会出现类似Syntax error: "(" unexpected的错误,一般这种是因为sh与bash有 ...
- Spring Boot入门之Hello World
Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不 ...