《华为机试》刷题之HJ98 自动售货系统

一、题目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、示例

在这里插入图片描述

三、代码

def fun_r(temp): # 初始化函数
    b = temp.split()
    m = b[1].split('-')
    q = b[2].split('-')
    dic_n['A1'], dic_n['A2'], dic_n['A3'] = int(m[0]), int(m[1]), int(m[2])
    dic_n['A4'], dic_n['A5'], dic_n['A6'] = int(m[3]), int(m[4]), int(m[5])
    dic_q['1'], dic_q['2'] = int(q[0]), int(q[1])
    dic_q['5'], dic_q['10'] = int(q[2]), int(q[3])
    print('S001:Initialization is successful')


def fun_p(money, temp): # 投币函数
    if temp not in [1, 2, 5, 10]:
        print('E002:Denomination error')
    elif temp > (dic_q['1'] + dic_q['2']*2):
        print('E003:Change is not enough, pay fail')
    elif sum(dic_n.values()) == 0:
        print('E005:All the goods sold out')
    else:
        dic_q[str(temp)] += 1
        money += temp
        print('S002:Pay success,balance={}'.format(money))
    return money

def fun_b(money, temp): # 购物函数
    if temp not in dic_n.keys():
        print('E006:Goods does not exist')
    elif dic_n[temp] == 0:
        print('E007:The goods sold out')
    elif money < dic_m[temp]:
        print('E008:Lack of balance')
    else:
        money = int(money) - dic_m[temp]
        print('S003:Buy success,balance={}'.format(money))
        dic_n[temp] -= 1
    return money

def fun_c(money): # 找零函数
    w10, w5, w2, w1 = 0, 0, 0, 0
    while money > 0:
        if money >= 10 and dic_q['10'] >= 1:
            money -= 10
            w10 += 1
            dic_q['10'] -= 1
        elif money >= 5 and dic_q['5'] >= 1:
            money -= 5
            w5 += 1
            dic_q['5'] -= 1
        elif money >= 2 and dic_q['2'] >= 1:
            money -= 2
            w2 += 1
            dic_q['2'] -= 1
        elif money >= 1 and dic_q['1'] >= 1:
            money -= 1
            w1 += 1
            dic_q['1'] -= 1
        else:
            money -= 1
    print('1 yuan coin number={}'.format(w1))
    print('2 yuan coin number={}'.format(w2))
    print('5 yuan coin number={}'.format(w5))
    print('10 yuan coin number={}'.format(w10))
    return money

def fun_q(temp): # 查询函数
    if ' ' not in temp:
        print('E010:Parameter error')
    elif item.split()[1] not in ['0', '1']:
        print('E010:Parameter error')
    elif item.split()[1] == '0':
        print('A1 2 {}'.format(dic_n['A1']))
        print('A2 3 {}'.format(dic_n['A2']))
        print('A3 4 {}'.format(dic_n['A3']))
        print('A4 5 {}'.format(dic_n['A4']))
        print('A5 8 {}'.format(dic_n['A5']))
        print('A6 6 {}'.format(dic_n['A6']))
    elif item.split()[1] == '1':
        print('1 yuan coin number={}'.format(dic_q['1']))
        print('2 yuan coin number={}'.format(dic_q['2']))
        print('5 yuan coin number={}'.format(dic_q['5']))
        print('10 yuan coin number={}'.format(dic_q['10']))

while True:
    try:
        s = input().split(';')
        dic_m = {'A1': 2, 'A2': 3, 'A3': 4, 'A4': 5, 'A5': 8, 'A6': 6}
        dic_n = {'A1': 0, 'A2': 0, 'A3': 0, 'A4': 0, 'A5': 0, 'A6': 0}
        dic_q = {'10': 0, '5': 0, '2': 0, '1': 0} 
        money = 0
        for item in s[:-1]:
            if item[0] == 'r': # 初始化
                fun_r(item)
            elif item[0] == 'p': # 投币
                temp = int(item.split()[1])
                money = fun_p(money, temp)
            elif item[0] == 'b': # 购物
                temp = item.split()[1]
                money = fun_b(money, temp)
            elif item[0] == 'c': # 找零
                if money == 0:
                    print('E009:Work failure')
                else:
                    money = fun_c(money) 
            elif item[0] == 'q': # 查询
                fun_q(item)
    except:
        break

四、算法说明

  • 静下来理解题目要求;
  • 按部就班,分模块写;
  • 相信自己,可以的!

本程序已经按照子函数形式,书写的很明白了,具体含义不在赘述

胡萝卜

2022年3月12日23:08:04

我不知道将去向何方,但我已在路上!
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 !
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

O_胡萝卜_O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值