【codewar-6kyu】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 single10050 or 25 dollars bill. A "Avengers" ticket costs 25 dollars.this

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?spa

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

Examples:

1 ### Python ###
2 
3 tickets([25, 25, 50]) # => YES 
4 tickets([25, 100]) # => NO. 

## 思路分析

在这倒编码毫无难度纯看逻辑的题上,竟然卡了半天,整我的都很差了,没好好读题没好好思考,让我去死一死blog

卖25块一张的票,可能会收到2五、50、100的钱;ci

售票员不带零钱,面对什么样的队伍才能找得开零钱,全卖得了票呢?it

要按顺序卖票!!class

收的钱就留下了!!sed

25开心手下;50必须找25;100优先找50,没有就找3张25(固然优先找50啦!50的只能找给100的,25还得尽可能留着给50的呢!在这儿卡了是否是傻……)di

## 代码解析

Python:

 1 def tickets(people):
 2     n25 = n50 = n100 = 0
 3     for e in people:
 4         if   e==25            : n25+=1
 5         elif e==50            : n25-=1; n50+=1
 6         elif e==100 and n50>0 : n25-=1; n50-=1
 7         elif e==100 and n50==0: n25-=3
 8         if n25<0 or n50<0:
 9             return 'NO'
10     return 'YES'

就是简简单单按照收钱找钱的过程来就行了,少年不要想太多。

相关文章
相关标签/搜索