Runoob Python100例

题目 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

·分析:1.排列组合
2.剔除不符合部分
注意:1-4 == range (1,5)

sum=0
for i in range(1,5):
 for j in range(1,5):
  for k in range(1,5):
   if (i!=j) and (k!=j) and (i!=k):
    print (i,j,k)
    sum+=1
print(sum)

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

·分析:1.分区段
2.输入 py3 的input函数:<>强制类型转换 int float eval(去双引号)

a=input()
2
type(a)
<class ‘str’>

参考:https://blog.csdn.net/qq_29883591/article/details/78177279

注意:算法优化。

#利润 l

l=float(input("请输入利润:"))
print("show me your money:",l)
bonus10w = 100000 * 0.1
bonus20w = bonus10w + 100000 * 0.500075
bonus40w = bonus20w + 200000 * 0.5
bonus60w = bonus40w + 200000 * 0.3
bonus100w = bonus60w + 400000 * 0.15

if l <=100000:
    bonus=l*0.1

elif l <=200000:
    bonus= bonus10w + (l-100000)*0.075

elif l <=400000:
    bonus= bonus20w + (l-200000)*0.05

elif l <=600000:
    bonus= bonus40w + (l-400000)*0.03

elif l <=1000000:
    bonus= bonus60w + (l-600000)*0.015

else:
    bonus= bonus100w + (l-1000000)*0.01

print ("bonus",bonus)

改进之后,https://blog.csdn.net/weixin_41084236/article/details/81564963#003_68

profit=int(input('Show me the money: '))
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]
print(bonus)


题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

·分析:1.x+100=y
2.y^ 2+168=z^2
->即 x+268=z^2

判断是否是完全平方数,最简单的方法是:平方根的值小数为0即可。

i **0.5 == int (i **0.5)
法一: 缺点:负整数无法得出。

import math
for i in range(1,100000):
    x = int(math.sqrt(i+100)) #
    y = int(math.sqrt(i+268)) #
    if (x * x == i + 100) and (y *y == i + 268 ):
        print (i)

法二:思路是:最坏的结果是n的平方与(n+1)的平方刚好差168,由于是平方的关系,不可能存在比这更大的间隙。

n=0
while (n+1)**2-n*n<=168:
    n+=1

for i in range((n+1)**2):
    if i**0.5==int(i**0.5) and (i+168)**0.5==int((i+168)**0.5):
        print(i-100)

猜你喜欢

转载自blog.csdn.net/weixin_44719417/article/details/90410283