算法题1——回文相关

题目:一个数如果从左向右读和从右向左读都一样,我们称其为回文数。例如66到999之间有10个回文数,请问在1到1000之间共有多少个回文数?

解法:

  1. 遍历1到1000的所有数,检查每个数是否为回文数。

  2. 计数回文数的数量。

def is_palindrome(num):
    return str(num) == str(num)[::-1]


count = 0
for i in range(1, 1001):
    if is_palindrome(i):
        count += 1

print(count)

 

posted @ 2024-06-14 10:31  Alieen617  阅读(2)  评论(0编辑  收藏  举报