Discuss / Python / 数学方法

数学方法

Topic source

RayPotter

#1 Created at ... [Delete] [Delete and Lock User]
def is_palindrome(n):
    s = n
    y = 0
    while s != 0:
        y = y*10 + s%10
        s = s//10
    
    return y == n
output = filter(is_palindrome, range(1, 1000)) 
print(list(output)) 

楼上很多利用转为字符串的方法很巧妙,我一开始也是想到了转为字符串,这里给大家提供另一种思路,利用数学方法进行判断。

厉害!


  • 1

Reply