首页 > 代码库 > 【codewar-6kyu】Vasya - Clerk
【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 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
.
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‘
就是简简单单按照收钱找钱的过程来就好了,少年不要想太多。
【codewar-6kyu】Vasya - Clerk