题解 | #自动售货系统#

自动售货系统

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

from sys import flags
dict_goods = {
    "A1":0,
    "A2":0,
    "A3":0,
    "A4":0,
    "A5":0,
    "A6":0
}


dict_prices = {
    "A1":2,
    "A2":3,
    "A3":4,
    "A4":5,
    "A5":8,
    "A6":6
}


dict_money = {
    1:0,
    2:0,
    5:0,
    10:0
}


def sold_out(*good):
    # 可变参数总是元组形式
    if good[0] in dict_goods:
        if dict_goods[good[0]] == 0:
            return True
    for tmp in dict_goods:
        if dict_goods[tmp] != 0:
            return False
    return True
        

rest_money = 0


class found(Exception):
    pass


def refund():
    result_10 = 0
    result_5 = 0
    result_2 = 0
    result_1 = 0
    flag = False
    rest_now = rest_money
    # print(f'rest_now:{rest_now}')
    try:
        for n10 in range(dict_money[10],-1,-1):
            # 四个数据位一组 需要每轮循环重置一次
            result_10 = n10
            result_5 = 0
            result_2 = 0
            result_1 = 0
            rest_now = rest_money
            # 从10块钱找起时恢复余额 在第一趟找不到的时候重新找
            if rest_now == n10*10:
                # rest_now 等于 当前用于找零的10块钱总额 找零成功
                rest_now-=n10*10
                result_10 = n10
                flag = True
                raise found
            elif rest_now < n10*10:
                # 10块找多了 得继续减少
                continue
            else:
                # rest_now 大于 当前用于找零的10块钱总额
                rest_now-=n10*10
                result_10 = n10
                # 检查下一种面值前 记录当前取用的值
            for n5 in range(dict_money[5],-1,-1):
                if rest_now == n5*5:
                    rest_now -= n5*5
                    result_5 = n5
                    flag = True

                    raise found

                elif rest_now < n5*5:
                    continue
                else:
                    rest_now -= n5*5
                    result_5 = n5
                for n2 in range(dict_money[2],-1,-1):
                    if rest_now == n2*2:
                        rest_now -= n2*2
                        result_2=n2

                        flag = True

                        raise found

                    elif rest_now < n2*2:
                        continue
                    else:
                        rest_now -= n2*2
                        result_2 = n2
                    for n1 in range(dict_money[1],-1,-1):
                        if rest_now == n1*1:
                            rest_now -= n1*1
                            result_1=n1
                            flag = True

                            raise found

                        elif rest_now < n1*1:
                            continue
                        else:
                            # rest_now > n1*1 说明1块钱不够找 break进下一轮2块钱循环        
                            result_1 = n1
                            break
    except found:
        print(f'{1} yuan coin number={result_1}')
        print(f'{2} yuan coin number={result_2}')
        print(f'{5} yuan coin number={result_5}')
        print(f'{10} yuan coin number={result_10}')
        dict_money[1]-=result_1
        dict_money[2]-=result_2
        dict_money[5]-=result_5
        dict_money[10]-=result_10




while True:
    try:
        orders = input().split(';')[:-1]
        # print(orders)
        for order in orders:
            if order[0] == 'r':
                # 执行初始化
                list_g = order.split(' ')[1].split('-')
                list_m = order.split(' ')[2].split('-')
                # print(f'list_g {list_g} list_m {list_m}')

                i = 0
                for tmp in dict_goods.keys():
                    dict_goods[tmp] = int(list_g[i])
                    i+=1

                i = 0
                for tmp in dict_money.keys():
                    dict_money[tmp] = int(list_m[i])
                    i+=1
                # print(dict_goods)
                # print(dict_money)
                print("S001:Initialization is successful")

                
            elif order[0] == 'p':
                # # print(f'order {order}')
                # # 检测投币
                money = int(order.split(' ')[1])
                # # print(f'money {money}')
                if money not in dict_money.keys():
                    # 1.非法输入 报错
                    print('E002:Denomination error')
                elif money != 1 and money != 2 and 1*dict_money[1]+2*dict_money[2] < money:
                    # ? 2.零钱不足
                    print("E003:Change is not enough, pay fail")
                elif sold_out(0):
                    # 3.售罄 报错
                    print('E005:All the goods sold out')
                else:
                    # 4.成功
                    rest_money+=money
                    dict_money[money]+=1
                    # 存入余额
                    print(f"S002:Pay success,balance={rest_money}")
                pass


            elif order[0] == 'b':
                # print(f'order :{order}')
                # 购买商品
                good = order.split(' ')[1]
                # print(f'good:{good}')
                if good not in dict_goods:
                    # 1.不在列表 
                    print('E006:Goods does not exist')
                elif sold_out(good):
                    # 2.售罄
                    print('E007:The goods sold out')
                elif rest_money < dict_prices[good]:
                    # 3.余额不足
                    print('E008:Lack of balance')
                else:
                    # 4.购买成功
                    
                    rest_money-=dict_prices[good]
                    dict_goods[good]-=1
                    print(f'S003:Buy success,balance={rest_money}')
            elif order[0] == 'c':
                # 退币
                if rest_money == 0:
                    # 1.余额不足
                    print('E009:Work failure')
                else:
                    # 2.退币 打印退币信息
                    refund()
                    # 重置余额
                    rest_money = 0
                    pass
            elif order[0] == 'q':
                # print(f'order [q] {order}')
                if len(order) == 3:
                    list_sorted = []
                    for good in dict_goods:
                        list_sorted.append([good, dict_prices[good],dict_goods[good]])
                    list_sorted.sort(key=lambda x:x[2],reverse=False)
                    # print('sorted success')
                    # 按余量排序 升序 余量一致时保持原商品名顺序
                    if order[2] == '0':
                        # print("0 success")
                        for good in list_sorted:
                            # print(f'good:{good}')
                            print(' '.join(list(map(str,good))))
                        # 0查询商品
                    elif order[2] == '1':
                        # print("1 success")

                        for money in dict_money:
                            print(f'{money} yuan coin number={dict_money[money]}')
                        # 1查询零钱
                else:
                    print("E010:Parameter error")
        
    except:
        break

全部评论

相关推荐

11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
勇敢的联想人前程似锦:如果我是你,身体素质好我会去参军,然后走士兵计划考研211只需要200多分。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务