''' Author: SJ2050 Date: 2021-10-16 22:24:48 LastEditTime: 2021-10-16 22:44:04 Version: v0.0.1 Description: Solution for homework3. Copyright © 2021 SJ2050 ''' def judgement(x): s = 0 p = 0 if x == 0: return 0 if x <= 10: s = 0 p = 0.1 elif x > 10 and x <= 20: s = 10 p = 0.075 elif x > 20 and x <= 40: s = 20 p = 0.05 elif x > 40 and x <= 60: s = 40 p = 0.03 elif x > 60 and x <= 100: s = 60 p = 0.015 else: s = 100 p = 0.01 return (x-s)*p+judgement(s) import bisect # 使用列表来实现判断 def judgementList(x): a = [0, 10, 20, 40, 60, 100] p = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01] ind = bisect.bisect_left(a, x) res = 0 res += (x-a[ind-1])*p[ind-1] for i in range(1, ind): res += (a[i]-a[i-1])*(p[i-1]) return res if __name__ == '__main__': x = 100 print(f'利润为:{x}w, 发放的奖金为{judgement(x)}w.') print('----------------------------------------') print('以下是使用列表计算的结果:') print(f'利润为:{x}w, 发放的奖金为{judgementList(x)}w.')