马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不二如是 于 2022-9-13 16:29 编辑
一星答案:
def tickets(people):
n25 = n50 = n100 = 0
for e in people:
if e == 25:
n25 += 1
elif e == 50:
n25 -= 1
n50 += 1
elif e == 100 and n50 > 0:
n25 -= 1
n50 -= 1
elif e == 100 and n50 == 0:
n25 -= 3
if n25 < 0 or n50 < 0:
return 'no'
return 'yes'
print(tickets([25, 25, 50])) # yes
print(tickets([50, 50, 100])) # no
二星答案:
# 用列表来存储收入的 25和50 两种面额,找零时删除,当列表不足够找零时break
def tickets(p):
"""传入排队人员钞票面值列表 p(25,50,100 三种面值),判断能否成功找零"""
saves = []
for i in p:
if i == 25:
saves.append(i)
elif i == 50 and saves.count(25):
saves.remove(25)
saves.append(50)
elif i == 100 and saves.count(50) and saves.count(25):
saves.remove(50)
saves.remove(25)
elif i == 100 and saves.count(25) >= 3:
for j in range(3):
saves.remove(25)
else:
break
else:
print('yes')
return
print('no')
三星答案:
基础语法:
算法讲解:
|