Functions related topics written program

Functions related topics written program

Topic Requirements:
For a positive decimal integer, define f (n) and for which the square of the digits, such as:
F (13 is). 1 ** = 2 + 2 = 10. 3 **
F (207) = 2 + 0 * 2 ** * 7 ** 2 + 2 = 53
is given below three positive integer k, a, b, you need to calculate the number of positive integer n satisfies a <= n <= b, and k * f (n) = n
input :
the first line contains three positive integer k, a, b, k> = 1, a, b <= 10 ** 18, a <= b;
output:
output a corresponding answer;
example:
input: 51500010000 # 51 * f (n) = n 5000 <= 10000 <= n
output: 3

code show as below:

def f(n):
    # 1.先把数字转换成字符串
    n = str(n)
    # 2.计算字符串中每个数的平发
    sum = 0
    for item in n:
        sum += int(item) ** 2
    return sum
# print(f(13))
# print(f(207))

# 1.接收变量k a b
s = input('')  # '51 5000 10000' ==['51','5000','10000']
print(type(s))

li = []
for item in s.split():
    li.append(int(item))
print(li)
k, a, b = li
print(k, a, b)

# 2.进行判断是否满足条件
count = 0
for i in range(a, b + 1):
    if k * f(i) == i:
        count += 1

print(count)

Output:

51 5000 10000
<class 'str'>
[51, 5000, 10000]
51 5000 10000
3
Published 60 original articles · won praise 6 · views 1346

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/103713053