Runoob Python100 cases

There are four digital title: 1,2,3,4, can be composed of how many other with no repeat triple-digit figures? Is the number?

* Analysis: 1 permutations
2. culling does not comply with
Note: 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)

Title company issued bonus commission based on profit. When profit (I) is less than or equal to 10 million, 10% of the prize can be mentioned; when profit than 10 million, less than 20 million, 10 million commission portion is less than 10%, higher than 100,000 yuan portion, may be 7.5% commission; is between 200,000 to 400,000, more than 20 million portion, may be 5% commission; when more than 40 million portion between 400,000 to 600,000, may be 3% commission ; is between from 600,000 to 1,000,000, more than 60 million portion may commission 1.5% above 100 million, more than one million yuan portion 1% commission, from the keyboard month profit I, should seek The total number of paid bonuses?

* Analysis: a segmented
2. Enter py3 the input function: <> cast int float eval (to double quotes)

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

Reference: https://blog.csdn.net/qq_29883591/article/details/78177279

Note: algorithm optimization.

#利润 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)

After improvements, 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)


An integer title, after 100 plus it is a perfect square, plus 168 is a perfect square, what is the number is how much?

* Analysis: Y = 100 + 1.x
2.y ^ 2 + 2 ^ 168 = Z
-> i.e. x + 268 = z ^ 2

Determines whether a perfect square, the easiest way is: the value of the square root is 0 to decimal.

i ** 0.5 == int (i ** 0.5)
Method a: drawback: not draw negative integer.

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)

Method two: The idea is: worst result is that the square of n (n + 1) is just the square of the difference 168, since the relationship of the square, there can be greater than this gap.

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)

Guess you like

Origin blog.csdn.net/weixin_44719417/article/details/90410283