题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

# 难倒是不难,就是有点麻烦,题目长,要按部就班地写
import sys

inits = []
commands = []

def withdraw(cash_remain, pocket, out={1:0, 2:0, 5:0, 10:0}):
    coins = [10, 5, 2, 1]
    i = 0
    while True:
        if i < 4:
            coin = coins[i]
            if cash_remain >= coin and pocket[coin] > 0:
                cash_remain -= coin
                out[coin] += 1
                pocket[coin] -= 1
            else:
                i += 1
        if sum(pocket.values()) == 0 or cash_remain == 0:
            break
    return cash_remain, pocket, out

for line in sys.stdin:
    line = line.strip()
    if line:
        tmp = ''
        cmds = ''
        for i in range(len(line)):
            if line[i] != ';':
                tmp += line[i]
            else:
                cmds = line[i+1:-1]
                break
        # print(tmp)
        # print(cmds)
        tmp = tmp.split()
        tmp[1:] = [tmp[1].split('-'), tmp[2].split('-')]
        inits = tmp
        cmds = cmds.split(';')
        # print(cmds)
        commands = cmds

# print(inits, commands)
goods = {'A'+str(i):int(inits[1][i-1]) for i in range(1, len(inits[1])+1)}
prices = {'A'+str(i+1):v for i, v in enumerate([2, 3, 4, 5, 8, 6])}
pocket = {1:int(inits[2][0]), 2:int(inits[2][1]), 5:int(inits[2][2]), 10:int(inits[2][3])}
cash_remain = 0
if inits[0] == 'r':
    print('S001:Initialization is successful')

# print(goods, prices, pocket)

for cmd in commands:
    if cmd[0] == 'p':
        cash = int(cmd[2:])
        if cash not in pocket:
            print('E002:Denomination error')
        else:
            pocket_1_2 = pocket[1] * 1 + pocket[2] * 2
            goods_num = sum(goods.values())
            if cash > pocket_1_2:
                print('E003:Change is not enough, pay fail')
            elif goods_num == 0:
                print('E005:All the goods sold out')
            else:
                cash_remain += cash
                pocket[cash] += 1
                print(f'S002:Pay success,balance={cash_remain}')
    elif cmd[0] == 'b':
        good = cmd[2:]
        if good not in goods:
            print('E006:Goods does not exist')
        elif goods[good] == 0:
            print('E007:The goods sold out')
        elif cash_remain < prices[good]:
            print('E008:Lack of balance')
        else:
            goods[good] -= 1
            cash_remain -= prices[good]
            print(f'S003:Buy success,balance={cash_remain}')
    elif cmd[0] == 'c':
        if cash_remain == 0:
            print('E009:Work failure')
        else:
            # print('start withdraw', cash_remain, pocket)
            iout = {1:0, 2:0, 5:0, 10:0}
            remain_cash, remain_pocket, out = withdraw(cash_remain, pocket, iout)
            cash_remain, pocket = remain_cash, remain_pocket
            # print('end withdraw', cash_remain, pocket)
            for i in out:
                print(str(i) + ' yuan coin number=' + str(out[i]))
    elif cmd[0] == 'q':
        flag = cmd[2:]
        if flag not in ['0', '1']:
            print('E010:Parameter error')
        elif flag == '0':
            goods_out = [[good, prices[good], goods[good]] for good in goods]
            goods_out.sort(key=lambda x: x[2], reverse=True)
            for good in goods:
                print(good + ' ' + str(prices[good]) + ' ' + str(goods[good]))
        else:
            for coin in pocket:
                print(str(coin) + ' yuan coin number=' + str(pocket[coin]))

全部评论

相关推荐

无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务