题解 | #自动售货系统#

自动售货系统

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

#直接按业务需求写代码就行了就是要细心
#不小心被图片坑了,初始化用了10,5,2,1的顺序,搞反了
#(1 元张数 -2 元张数 -5 元张数 -10 元张数)
import sys
class Mac():
    def __init__(self, nums, coinnums):
        self.goods = ["A1", "A2", "A3", "A4", "A5", "A6"]
        self.prices = [2, 3, 4, 5, 8, 6]
        self.goodsnums = list(map(int,nums))
        self.cointype = [10, 5, 2, 1]
        self.coinnums = list(map(int,coinnums[::-1]))
        self.balance = 0
        print("S001:Initialization is successful")

    def pay(self, money):
        money = int(money)
        if money not in self.cointype:
            print("E002:Denomination error")
        elif money > 2 and money > self.cointype[2] * self.coinnums[2] + \
                self.cointype[3] * self.coinnums[3]:
            print("E003:Change is not enough, pay fail")
        elif sum(self.goodsnums) == 0:
            print("E005:All the goods sold out")
        else:
            self.balance += money
            self.coinnums[self.cointype.index(money)] += 1
            print("S002:Pay success,balance={}".format(self.balance))

    def buy(self, good):
        if good not in self.goods:
            print("E006:Goods does not exist")
        elif self.goodsnums[self.goods.index(good)] == 0:
            print("E007:The goods sold out")
        elif self.balance < self.prices[self.goods.index(good)]:
            print("E008:Lack of balance")
        else:
            self.balance -= self.prices[self.goods.index(good)]
            self.goodsnums[self.goods.index(good)] -= 1
            print("S003:Buy success,balance={}".format(self.balance))

    def quit(self):
        if self.balance == 0:
            print("E009:Work failure")
            return
        tuibi = [0,0,0,0]
        index = 0
        while self.balance > 0 and index <= 3:
            num = self.balance // self.cointype[index]

            if self.coinnums[index] >= num:
                self.coinnums[index] -= num
            else:
                num = self.coinnums[index]
                self.coinnums[index] = 0
            tuibi[index] = num
            self.balance -= tuibi[index] * self.cointype[index]
            index += 1
        cointype = self.cointype[::-1]
        tuibi = tuibi[::-1]
        for i in cointype:
            print("{} yuan coin number={}".format(i, tuibi[cointype.index(i)]))
        self.balance = 0
#         print(self.coinnums)

    def query(self, q_type):
        if q_type == '0':
            goods = []
            for i in range(len(self.goods)):
                goods.append([self.goods[i], self.prices[i], self.goodsnums[i]])
            goods.sort(key = lambda x: x[2], reverse = True)
            for g in goods:
                print(" ".join(map(str, g)))
        elif q_type == '1':
            cointype = self.cointype[::-1]
            coinnums = self.coinnums
            for i in range(len(cointype)):
                print("{} yuan coin number={}".format(cointype[i], coinnums[i]))
        else:
            print("E010:Parameter error")



while True:
    try:
        cmds = input().split(';')
        init = cmds[0].split()
        mac = Mac(init[1].split('-'), init[2].split('-'))

        for cmd in cmds[1:]:
            if 'p' in c***c.pay(cmd.split()[1])
            elif 'b' in c***c.buy(cmd.split()[1])
            elif 'c' in c***c.quit()
            elif 'q' in cmd:
                q = cmd.split()
                if len(q) > 1:
                    mac.query(q[1])
                else:
                    mac.query("error")



    except:
#         print(sys.exc_info())
        break










全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
11-27 17:08
已编辑
牛客_产品运营部_私域运营
腾讯 普通offer 24k~26k * 15,年包在36w~39w左右。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务