题目:
这道题算是我的老朋友了,光靠在脑子里面推,我做不出来,以下借助类似分析:
假如我的志愿时数为120h,那么比例折合后为 50*0.2 + 50*0.15 + 20*0.1 = 19.5
对式子再详细分析 (50-0)*0.2 + (100-50)*0.15 + (120-100) *0.1 = 19.5
我是这样理解,区间之间都有上限和下限,120在(0-50)和(50-100)的区间满了,那就区间内的总数与比例相乘,得到结果;
而120在(100-150)的区间没有满,此时区间下限为100,120-100得到区间内的总数,最后相加,即可。
运用到上题也一样。
我的代码:
def func(profit):
if profit <= 100000:
get = profit*0.1
elif 100000 < profit and profit < 200000:
get = 100000*0.1+(profit-100000)*0.075
elif 200000 <= profit and profit < 400000:
get = 100000*0.1+200000*0.075+(profit-200000)*0.005
elif 400000 <= profit and profit < 600000:
get = 100000*0.1+200000*0.075+200000*0.005+(profit-400000)*0.003
elif 600000 <= profit and profit < 1000000:
get = 100000*0.1+200000*0.075+200000*0.005+200000*0.03+(profit-400000)*0.015
else:
get = 100000*0.1+200000*0.075+200000*0.005+200000*0.03+400000*0.015+(profit-1000000)*0.01
return get
wage = func(140000)
print(f"本月奖金为{wage}元")
参考答案:
def func(profit):
bonus = 0
thresholds = [100000, 100000, 200000, 200000, 400000]
rates = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
for i in range(len(thresholds)):
if profit <= thresholds[i]:
bonus += profit*rates[i]
profit = 0
break
else:
bonus += thresholds[i]*rates[i]
profit -= thresholds[i]
bonus += profit*rates[-1]
return bonus
print(f"本月奖金为{func(140000)}元")