Python-special palindrome

Problem description
  123321 is a very special number, it reads the same from the left and read from the right.
  Enter a positive integer n, program to find all such five-digit and six-digit decimal numbers, satisfying that the sum of all digits is equal to n.
Input format
  Enter a line, including a positive integer n.
Output format
  Output the integers that meet the conditions in ascending order, each integer occupies one line.
Sample input
52
Sample output
899998
989989
998899
Data scale and convention
  1 <= n <= 54.

Code:

n = int(input())

def f(s):   #判断是否满足和
    cnt = 0
    for i in s:
        cnt += eval(i)
    return cnt == n


for i in range(10000, 1000000):
    s = str(i)  #将数字转为字符串
    if s == s[::-1] and f(s):   #切片操作来判断是否为回文数
        print(i)
Published 15 original articles · praised 6 · visits 38

Guess you like

Origin blog.csdn.net/weixin_46165788/article/details/105523678