题解 | #自动售货系统#
自动售货系统
http://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
真是写的我头皮发麻!!
# -*- coding: utf-8 -*- """ Created on Mon Jan 17 23:05:29 2022 @author: Jack """ while True: try: class shangpin: def __init__(self,price,num): self.price=price self.num=num self.su=self.num*self.price class money: def __init__(self,miane,num): self.miane=miane self.num=num def su(self): return self.num * self.miane n=input().split(';') balance=0 ini=n[0].split() ini[1]=list(map(int,ini[1].split('-'))) ini[2]=list(map(int,ini[2].split('-'))) goods=[] a1=shangpin(2,ini[1][0]) goods.append(['A1',a1]) a2=shangpin(3,ini[1][1]) a3=shangpin(4, ini[1][2]) a4=shangpin(5, ini[1][3]) a5=shangpin(8, ini[1][4]) a6=shangpin(6, ini[1][5]) m1=money(1, ini[2][0]) m2=money(2, ini[2][1]) m5=money(5, ini[2][2]) m10=money(10, ini[2][3]) goods.append(['A2',a2]) goods.append(['A3',a3]) goods.append(['A4',a4]) goods.append(['A5',a5]) goods.append(['A6',a6]) print('S001:Initialization is successful') for i in n[1:len(n)-1]: if i=='c': t10=0 t1=0 t2=0 t5=0 if balance==0: print('E009:Work failure') if balance!=0: if balance>=10: for j in range(m10.num+1): if j*10<=balance: t10=max(t10,j) balance=balance-10*t10 m10.num=m10.num-t10 if balance>=5: for k in range(m5.num+1): if k*5<=balance: t5=max(t5,k) balance=balance-5*t5 m5.num=m5.num-t5 if balance>=2: for l in range(m2.num+1): if l*2<=balance: t2=max(t2,l) balance=balance-2*t2 m2.num=m2.num-t2 if balance>=1: for m in range(m1.num+1): if m*1<=balance: t1=max(t1,m) balance=balance-1*t1 m1.num=m1.num-t1 print('1 yuan coin number=%d' %t1) print('2 yuan coin number=%d' %t2) print('5 yuan coin number=%d' %t5) print('10 yuan coin number=%d' %t10) if i[0]=='p': tb=i.split() if tb[1] not in ['1','2','5','10']: print('E002:Denomination error') elif m1.su()+m2.su()<int(tb[1]) and tb[1] not in ['1','2']: print('E003:Change is not enough, pay fail') elif tb[1] in ['1','2']: balance+=int(tb[1]) print('S002:Pay success,balance=%d' %balance) if tb[1]=='1': m1.num+=1 else: m2.num+=1 elif a1.num==0 and a2.num==0 and a3.num==0 and a4.num==0 and a5.num==0 and a6.num==0: print('E005:All the goods sold out') else: balance+=int(tb[1]) print('S002:Pay success,balance=%d' %balance) if tb[1]=='10': m10.num+=1 else: m5.num+=1 if i[0]=='b': gm=i.split() if gm[1] not in ['A1','A2','A3','A4','A5','A6']: print('E006:Goods does not exist') for j in range(len(goods)): if gm[1]==goods[j][0]: if goods[j][1].num==0: print('E007:The goods sold out') break elif balance<goods[j][1].price: print('E008:Lack of balance') break else: goods[j][1].num=goods[j][1].num-1 balance=balance-goods[j][1].price print('S003:Buy success,balance=%d' %balance) break if i in ['q 0','q 1'] and i[0]=='q': cx=i if cx[1]=='0': print(cx[1]) for j in goods: x='' x+=j[0]+' '+str(j[1].price)+' '+str(j[1].num) print(x) if cx[1]=='1': print('1 yuan coin number=%d' %m1.num) print('2 yuan coin number=%d' %m2.num) print('5 yuan coin number=%d' %m5.num) print('10 yuan coin number=%d' %m10.num) if i[0]=='q' and i not in ['q 0','q 1']: print('E010:Parameter error') except(EOFError): break