codewars.com 价值6的The new “Avengers“ movie has just been released! 题目解答答案汇总

codewars.com 价值6的The new "Avengers" movie has just been released! 题目解答答案汇总

 

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 single 10050 or 25 dollar bill. An "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 every person and give change if he initially has no money and sells the tickets strictly in the order people queue?

Return YES, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return NO.

刚开始,不知道题目到底想解决什么,后来才明白,题目的意思是:

从队伍的第一个开始,按顺序卖票,一人只卖一票,要卖到最后一个,保证队伍里的每个人都买到票,拿着50和100的能得到找零。

那么就要用到遍历FOR,一开始拿50或者100的直接就NO了,因为一开始没钱找零。

理解题目后就容易了。

Examples:

tickets([25, 25, 50]) # => YES 
tickets([25, 100]) # => NO. Vasya will not have enough money to give change to 100 dollars
tickets([25, 25, 50, 50, 100]) # => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)

之前的几题解法比较统一简便,这题的分歧比较大点,摘录点高分答案下来自己分析一下:

def tickets(people):
  till = {100.0:0, 50.0:0, 25.0:0}#因为要找零,所以必须知道手上什么面值的。
    
  for paid in people:#开始遍历输入的数字(开始卖票,大家站好了!)
    till[paid] += 1#将面值数量更新到till字典(收了什么面值的钱记till本子上)
    change = paid-25.0#将需要找零的钱赋予change变量(需要给他找多少零呢?先记在change本子上)
    
    for bill in (50,25):#如果账单属于50和25—(先一个个试你要付出50还是25的找零)
      while (bill <= change and till[bill] > 0):#看看change本子上记着我们实际要找零多少,till本子上有没有收钱,够不够付给bill这账单。
        till[bill] -= 1 #够的话,直接付出对应的找零,从till去掉1个bill面值键对应的值,
        change -= bill#已经付出的找零要扣除后再计算,直到找零不等于0,就执行下一句

    if change != 0:#如果找零等于0,这句就不执行了,直接return 'yes'
      return 'NO'
        
  return 'YES'
#和上面解法不同的是,这个是一个个去判断,其实思路一样,写法不同。
def tickets(people):
    n25 = n50 = n100 = 0#这里用的是变量,不同于上面用字典。
    for n in people:#一个个的判断,客户有多少面值的钱,我有多少找零,够不够支付。
        if n == 25:
            n25 += 1
        elif n == 50 and n25 > 0:
            n25 -= 1
            n50 += 1
        elif n == 100 and n50 > 0 and n25 > 0:
            n25 -= 1
            n50 -= 1
            n100 += 1
        elif n == 100 and n50 == 0 and n25 >= 3:#网络上的解法这里少写了个=号,应该是>=3。
            n25 -= 3
            n100 += 1
        else:
            return "NO"
    return "YES"
还是一样的思路,和第二一样的判断,就是在返回写法上不一样。
def tickets(people):
    change = 'YES'
    twentyfive, fifty, onehundred = 0, 0, 0
    
    for cash in people:
        if change == 'NO':
            break

        if cash == 25:
            twentyfive += 1
        elif cash == 50 and twentyfive > 0:
            twentyfive -= 1
            fifty += 1
        elif cash == 100:
            if fifty > 0 and twentyfive > 0:
                fifty -= 1
                twentyfive -= 1
                onehundred += 1
            elif twentyfive > 2:
                twentyfive -= 3
                onehundred += 1
            else:
                change = 'NO'
        else:
            change = 'NO'
            
    return change
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值