题目:
企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
程序分析:
请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
个人解题思路:用if-elif 多分支处理
while True:
profit = input("请输入利润: (单位:万元)\n").strip()
if not profit:continue
if not profit.isdigit():
print("您的输入有误!请重新输入。")
continue
else:
profit = eval(profit)
bonus_a,bonus_b,bonus_c,bonus_d,bonus_e = 10*0.1,10*0.075,20*0.05,20*0.03,40*0.015
bonus_list = [bonus_a,bonus_b,bonus_c,bonus_d,bonus_e]
if profit > 100:
bonus = (profit - 100) * 0.01 + sum(bonus_list)
elif profit > 60:
bonus = (profit - 60) *0.015 + sum(bonus_list[:-1])
elif profit > 40:
bonus = (profit - 40) *0.03 + sum(bonus_list[:-2])
elif profit > 20:
bonus = (profit - 20) *0.05 + sum(bonus_list[:-3])
elif profit > 10:
bonus = (profit - 10) *0.075 + sum(bonus_list[:-4])
elif profit > 0:
bonus = profit *0.1
else:
print("没利润,没有奖金!")
continue
print("奖金是%s万元"%bonus)
其他解题参考:
money = int(input("输入利润:"))
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
level = [100,60,40,20,10,0]
s = 0
for i in range(6):
if money-level[i]>0:
s += (money-level[i])*rat[i]
money = level[i]
print (s)
(本文编号002,首发于2018年9月12日)