【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.

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.

Examples:

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

## 思路分析

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

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

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

要按顺序卖票!!

收的钱就留下了!!

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

## 代码解析

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'

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

 

转载于:https://www.cnblogs.com/pudding-ai/p/5824068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值