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:
本地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