题解 | #自动售货系统#

自动售货系统

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

#敲了两个多小时总算敲出来了,敲出来发现不如干脆写个售货机类。起初觉得这个退钱逻辑很蠢无法应对特殊情况,
#后来想想似乎特殊情况在投币时已经规避掉了。在题解区没找到灵感,后来人如果有更好的方法麻烦踢我一下。
price = {"A1": 2, "A2": 3, "A3": 4, "A4": 5, "A5": 8, "A6": 6}  # 商品价格表
number = {"A1": 0, "A2": 0, "A3": 0, "A4": 0, "A5": 0, "A6": 0}  # 商品数量表
rmb = {"1": 0, "2": 0, "5": 0, "10": 0}  # 钱包
balance = 0  # 当前余额


def r(s):
    s1, s2 = s.split()[1:]
    s1 = list(map(int, s1.split("-")))
    s2 = list(map(int, s2.split("-")))
    (
        number["A1"],
        number["A2"],
        number["A3"],
        number["A4"],
        number["A5"],
        number["A6"],
    ) = (s1[0], s1[1], s1[2], s1[3], s1[4], s1[5])
    rmb["1"], rmb["2"], rmb["5"], rmb["10"] = s2[0], s2[1], s2[2], s2[3]
    print("S001:Initialization is successful")


def p(s):
    s1 = int(s.split()[-1])
    if s1 not in [1, 2, 5, 10]:
        print("E002:Denomination error")
    elif s1 > 2 and rmb["1"] + rmb["2"] * 2 < s1:
        print("E003:Change is not enough, pay fail")
    elif sum(number.values()) == 0:
        print("E005:All the goods sold out")
    else:
        global balance
        balance += s1
        rmb[str(s1)] += 1
        print("S002:Pay success,balance=" + str(balance))


def b(s):
    global balance
    s1 = s.split()[-1]
    if s1 not in number.keys():
        print("E006:Goods does not exist")
    elif number[s1] == 0:
        print("E007:The goods sold out")
    elif balance < price[s1]:
        print("E008:Lack of balance")
    else:
        balance -= price[s1]
        number[s1] -= 1
        print("S003:Buy success,balance=" + str(balance))


def c():
    global balance
    n = balance
    if n == 0:
        print("E009:Work failure")
    else:
        n += 1
        arr = [0, 0, 0, 0]
        while arr[0] + arr[1] * 2 + arr[2] * 5 + arr[3] * 10 != n:
            n -= 1
            # arr = [0, 0, 0, 0]  后来想了一下不需要这一行
            while arr[3] * 10 <= n and arr[3] <= rmb["10"]:
                arr[3] += 1
            arr[3] -= 1
            while arr[2] * 5 + arr[3] * 10 <= n and arr[2] <= rmb["5"]:
                arr[2] += 1
            arr[2] -= 1
            while arr[1] * 2 + arr[2] * 5 + arr[3] * 10 <= n and arr[1] <= rmb["2"]:
                arr[1] += 1
            arr[1] -= 1
            while (
                arr[0] + arr[1] * 2 + arr[2] * 5 + arr[3] * 10 <= n
                and arr[0] <= rmb["1"]
            ):
                arr[0] += 1
            arr[0] -= 1
        balance = 0
        rmb["10"] -= arr[3]
        rmb["5"] -= arr[2]
        rmb["2"] -= arr[1]
        rmb["1"] -= arr[0]
        print("1 yuan coin number=" + str(arr[0]))
        print("2 yuan coin number=" + str(arr[1]))
        print("5 yuan coin number=" + str(arr[2]))
        print("10 yuan coin number=" + str(arr[3]))


def q(s):
    s1 = s.split()[-1]
    if s1 not in ["0", "1"]:
        print("E010:Parameter error")
    elif s1 == "0":
        for i, j in enumerate(sorted(number.items(), key=lambda x: x[1], reverse=True)):
            print(i, price[i], j)
    else:
        print("1 yuan coin number=" + str(rmb["1"]))
        print("2 yuan coin number=" + str(rmb["2"]))
        print("5 yuan coin number=" + str(rmb["5"]))
        print("10 yuan coin number=" + str(rmb["10"]))


s = input().split(";")
for i in s[:-1]:
    if i[0] == "p":
        p(i)
    elif i[0] == "b":
        b(i)
    elif i[0] == "c":
        c()
    elif i[0] == "q":
        q(i)
    elif i[0] == "r":
        r(i)
    else:
        continue

全部评论

相关推荐

11-09 12:17
清华大学 C++
out11Man:小丑罢了,不用理会
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务