【2019.07.23 Python每日一题】答案—— 企业发放的奖金根据利润提成

https://www.runoob.com/python/python-exercise-example2.html

题目:企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?

程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

答案:

解法一:(逆向列表)

i = eval(input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
    if i>arr[idx]:
        r+=(i-arr[idx])*rat[idx]
        print ((i-arr[idx])*rat[idx])
        i=arr[idx]
print(r)

 示例输出:

净利润:120000
1500.0
10000.0
11500.0

解法二:(正向列表)

profit=int(input('Show me the money: 万元'))
bonus=0
thresholds=[0,10,20,40,60,100]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(1, len(thresholds)):
    if profit < thresholds[i]:
        bonus += (profit-thresholds[i-1]) * rates[i-1]
        break   
    else:
        bonus += (thresholds[i]-thresholds[i-1]) * rates[i-1]
else:
    bonus += (profit-thresholds[-1]) * rates[-1]
print(bonus)

解法三:(正向差值列表)

profit=int(input('Show me the money(万元): '))
bonus=0
thresholds=[10,10,20,20,40]
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]
print(bonus)

Tips:

  1. 数据和算法的分离思想
  2. 循环处理每个分段,找到链条,并且找到最特殊的一个,作为开头或者结尾。
  3. 解法一:倒序排列表,且处理完每个分段,让其回归到下一个分段的节点(开头)

 

本地notebook链接:

http://localhost:8890/notebooks/notebooks/Python123/%E3%80%90%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%98%E3%80%91/%E3%80%902019.07.23%20Python%E6%AF%8F%E6%97%A5%E4%B8%80%E9%A2%98%E3%80%91%E7%AD%94%E6%A1%88%E2%80%94%E2%80%94%20%E4%BC%81%E4%B8%9A%E5%8F%91%E6%94%BE%E7%9A%84%E5%A5%96%E9%87%91%E6%A0%B9%E6%8D%AE%E5%88%A9%E6%B6%A6%E6%8F%90%E6%88%90.ipynb

 

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值