一、题目
二、示例
三、代码
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
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |