题解 | #自动售货系统# 找零的地方直接循环处理简单点
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
s = input().split(";")
# 商品
key = s[0].split(' ')
t1 = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6']
t2 = map(int, key[1].split('-'))
goods_ct = dict(zip(t1, t2))
goods_value = {'A1': 2, 'A2': 3, 'A3': 4, 'A4': 5, 'A5': 8, 'A6': 6}
# 钱
t1 = [1, 2, 5, 10]
t2 = map(int, key[2].split('-'))
money_ct = dict(zip(t1, t2))
print("S001:Initialization is successful")
# 投币额
global cost
cost = 0
def compute(order):
global cost
if order[0] == 'q':
if order == 'q 0':
for good in goods_value.keys():
print("{} {} {}".format(
good, goods_value[good], goods_ct[good]))
elif order == 'q 1':
for m in money_ct.keys():
print("{} yuan coin number={}".format(m, money_ct[m]))
else:
print("E010:Parameter error")
if order[0] == 'p':
cost_temp = int(order[2:])
if cost_temp not in [1, 2, 5, 10]:
print("E002:Denomination error")
elif cost_temp in [5, 10] and money_ct[1]*1 + money_ct[2]*2 < cost_temp:
print("E003:Change is not enough, pay fail")
elif sum(goods_ct.values()) == 0:
print("E005:All the goods sold out")
else:
cost += cost_temp
# 增加一张库存
money_ct[cost_temp] += 1
print(f"S002:Pay success,balance={cost}")
if order[0] == 'b':
name = order[2:]
if name not in goods_value.keys():
print("E006:Goods does not exist")
elif goods_ct[name] == 0:
print("E007:The goods sold out")
elif cost < goods_value[name]:
print("E008:Lack of balance")
else:
goods_ct[name] -= 1
cost -= goods_value[name]
print(f"S003:Buy success,balance={cost}")
if order[0] == 'c':
if cost == 0:
print("E009:Work failure")
else:
re_ct = dict(zip([1, 2, 5, 10], [0, 0, 0, 0]))
for n in [10, 5, 2, 1]:
while money_ct[n] > 0 and cost - n >= 0:
re_ct[n] += 1
money_ct[n] -= 1
cost -= n
for m in re_ct.keys():
print("{} yuan coin number={}".format(m, re_ct[m]))
# 命令
orders = s[1:-1]
for order in orders:
compute(order)
针对找零模块原来会遇到 8 = 5+2 而不是 8 = 2 + 2 +2 +2的情形增加了dfs回溯 这也太复杂了
r 22-18-21-21-7-20 0-23-10-6;p 2;p 2;p 2;p 2;c;
用这个自测看看你的是返回 2 5 还是 2 2 2 2 大部分人都不对 而且没有设计这样的用例 不是考点 只能说题目出的太难或者有点问题
s = input().split(";")
# 商品
key = s[0].split(' ')
t1 = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6']
t2 = map(int, key[1].split('-'))
goods_ct = dict(zip(t1, t2))
goods_value = {'A1': 2, 'A2': 3, 'A3': 4, 'A4': 5, 'A5': 8, 'A6': 6}
# 钱
t1 = [1, 2, 5, 10]
t2 = map(int, key[2].split('-'))
money_ct = dict(zip(t1, t2))
print("S001:Initialization is successful")
# 投币额
global cost
cost = 0
def compute(order):
global cost
global money_ct
global goods_ct
global res
if order[0] == 'q':
if order == 'q 0':
for good in goods_value.keys():
print("{} {} {}".format(
good, goods_value[good], goods_ct[good]))
elif order == 'q 1':
for m in money_ct.keys():
print("{} yuan coin number={}".format(m, money_ct[m]))
else:
print("E010:Parameter error")
if order[0] == 'p':
cost_temp = int(order[2:])
if cost_temp not in [1, 2, 5, 10]:
print("E002:Denomination error")
elif cost_temp in [5, 10] and money_ct[1]*1 + money_ct[2]*2 < cost_temp:
print("E003:Change is not enough, pay fail")
elif sum(goods_ct.values()) == 0:
print("E005:All the goods sold out")
else:
cost += cost_temp
# 增加一张库存
money_ct[cost_temp] += 1
print(f"S002:Pay success,balance={cost}")
if order[0] == 'b':
name = order[2:]
if name not in goods_value.keys():
print("E006:Goods does not exist")
elif goods_ct[name] == 0:
print("E007:The goods sold out")
elif cost < goods_value[name]:
print("E008:Lack of balance")
else:
goods_ct[name] -= 1
cost -= goods_value[name]
print(f"S003:Buy success,balance={cost}")
if order[0] == 'c':
if cost == 0:
print("E009:Work failure")
else:
re_ct = dict(zip([1, 2, 5, 10], [0, 0, 0, 0]))
# for n in [10, 5, 2, 1]:
# while money_ct[n] > 0 and cost - n >= 0:
# re_ct[n] += 1
# money_ct[n] -= 1
# cost -= n
# for m in re_ct.keys():
# print("{} yuan coin number={}".format(m, re_ct[m]))
res = []
tuibi(cost, money_ct, re_ct)
res.sort(key=lambda x: (x[0], sum(x[1].values())))
cost = res[0][0]
solution = res[0][1]
money_ct = res[0][2]
for m in solution.keys():
print("{} yuan coin number={}".format(m, solution[m]))
def tuibi(target, money_ct, solution):
for n in [10, 5, 2, 1]:
if money_ct[n] and target-n >= 0:
money_ct[n] -= 1
solution[n] += 1
tuibi(target-n, money_ct, solution)
# 还原
money_ct[n] += 1
solution[n] -= 1
# 结束标志
# 无钱可找
res.append([target, solution.copy(), money_ct.copy()])
# 命令
orders = s[1:-1]
for order in orders:
compute(order)

