题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

goods = {'A1':0,'A2':0,'A3':0,'A4':0,'A5':0,'A6':0}
money = {1:0,2:0,5:0,10:0}
price = {'A1':2,'A2':3,'A3':4,'A4':5,'A5':8,'A6':6}
yu = 0

def find(yu,a=0,b=0,c=0,d=0):
    #a,b,c,d = 0,0,0,0
    while yu>0:
        if yu>=10 and money[10]>0:
            d += 1
            money[10] -= 1
            yu -= 10
        elif yu>=5 and money[5]>0:
            c += 1
            money[5] -= 1
            yu -= 5
        elif yu>=2 and money[2]>0:
            b += 1
            money[2] -= 1
            yu -= 2
        elif yu>=1 and money[1]>0:
            a += 1
            money[1] -= 1
            yu -= 1
        elif yu>0 and money[1]==0:
            yu -= 1
    return a,b,c,d

s = input().split(';')
for i in s[:-1]:
    if i[0]=='r':
        l1,l2 = list(map(int,i.split()[1].split('-'))),list(map(int,i.split()[2].split('-')))
        goods['A1'],goods['A2'],goods['A3'],goods['A4'],goods['A5'],goods['A6'] = l1[0],l1[1],l1[2],l1[3],l1[4],l1[5]
        money[1],money[2],money[5],money[10] = l2[0],l2[1],l2[2],l2[3]
        print('S001:Initialization is successful')
    if i[0]=='p':
        if int(i[2:]) not in money:
            print('E002:Denomination error')
        elif money[1]+money[2]*2<int(i[2:]):
            print('E003:Change is not enough, pay fail')
        elif goods['A1']==0 and goods['A2']==0 and goods['A3']==0 and goods['A4']==0 and goods['A5']== 0 and goods['A6']==0:
            print('E005:All the goods sold out')
        else:
            yu += int(i[2:])
            money[int(i[2:])] += 1
            print('S002:Pay success,balance=%d'%yu)
    if i[0]=='b':
        if i[2:] not in goods:
            print('E006:Goods does not exist')
        elif goods[i[2:]]==0:
            print('E007:The goods sold out')
        elif yu<price[i[2:]]:
            print('E008:Lack of balance')
        else:
            yu -= price[i[2:]]
            goods[i[2:]] -= 1
            print('S003:Buy success,balance=%d'%yu)
    if i[0]=='c':
        if yu==0:
            print('E009:Work failure')
        else:
            a,b,c,d = find(yu)
            yu = 0
            print('1 yuan coin number=%d'%a)
            print('2 yuan coin number=%d'%b)
            print('5 yuan coin number=%d'%c)
            print('10 yuan coin number=%d'%d)
    if i[0]=='q':
        if i[1]!=' ' or i[2:] not in ['0','1']:
            print('E010:Parameter error')
        else:
            if i[2]=='0':
                lis = sorted(sorted(goods),key=lambda x:goods[x],reverse=True)
                for j in lis:
                    print(*[j,price[j],goods[j]])
            if i[2]=='1':
                for j in money:
                    print('%d yuan coin number=%d'%(j,money[j]))

全部评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务