python | 百职帮简单练习题(十):奖金几何

题目:

 


 这道题算是我的老朋友了,光靠在脑子里面推,我做不出来,以下借助类似分析:

假如我的志愿时数为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)}元")

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值