题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
#究极全面覆盖代码
def f(pq):
w10,w5,w2,w1 = 0,0,0,0
while pq>0:
if pq>=10 and dic_q['10']>=1:
pq -= 10
w10 += 1
dic_q['10'] -= 1
elif pq>=5 and dic_q['5']>=1:
pq -= 5
w5 += 1
dic_q['5'] -= 1
elif pq>=2 and dic_q['2']>=1:
pq -= 2
w2 += 1
dic_q['2'] -= 1
elif pq>=1 and dic_q['1']>=1:
pq -= 1
w1 += 1
dic_q['1'] -= 1
else:
pq -= 1
return pq,w1,w2,w5,w10
s = input().split(';')
dic_m = {'A1':2,'A2':3,'A3':4,'A4':5,'A5':8,'A6':6}
dic_n = {'A1':0,'A2':0,'A3':0,'A4':0,'A5':0,'A6':0}
dic_q = {'1':0,'2':0,'5':0,'10':0}
pq = 0
for i in s[:-1]:
if i[0] == 'r':
b = i.split()
shuliang = b[1].split('-')
q = b[2].split('-')
a = 0
for i in dic_n:
dic_n[i] = int(shuliang[a])
a += 1
c = 0
for i in dic_q:
dic_q[i] = int(q[c])
c += 1
print('S001:Initialization is successful')
elif i[0] == 'p':
pq1 = int(i[2:])
if pq1 not in[1,2,5,10]:
print('E002:Denomination error')
elif dic_q['1']+dic_q['2']*2<pq1 and pq1>2:
print('E003:Change is not enough, pay fail')
elif sum(dic_n.values())==0:
print('E005:All the goods sold out')
else:
pq += pq1
dic_q[str(pq1)] += 1
print('S002:Pay success,balance=%d'%pq)
elif i[0]=='b':
bn = i[2:]
if bn not in dic_m:
print('E006:Goods does not exist')
elif dic_n[bn]==0:
print('E007:The goods sold out')
elif dic_m[bn]>pq:
print('E008:Lack of balance')
else:
pq -= dic_m[bn]
dic_n[bn] -= 1
print('S003:Buy success,balance=%d'%pq)
elif i[0]=='c':
if pq==0:
print('E009:Work failure')
else:
pq,w1,w2,w5,w10 = f(pq)
print('1 yuan coin number=%d'%w1)
print('2 yuan coin number='+str(w2))
print('5 yuan coin number=%d'%w5)
print('10 yuan coin number=%d'%w10)
elif i[0] == 'q':
if ' ' not in i:
print('E010:Parameter error')
elif i[2] not in ['0','1']:
print('E010:Parameter error')
elif i[2]=='0':
good = sorted(dic_n,key=lambda x:dic_n[x],reverse=True)
for i in good:
print('%s %d %d'%i%dic_m[i]%dic_n[i])
elif i[2]=='1':
print('1 yuan coin number=%d'%dic_q['1'])
print('2 yuan coin number=%d'%dic_q['2'])
print('5 yuan coin number=%d'%dic_q['5'])
print('10 yuan coin number=%d'%dic_q['10'])