Python 判断回文数:代码实现及示例
def Ishuiwen(x): ' 将x转换为字符串,方便逆序排列 ' s = str(x) ' 将字符串逆序排列 ' s_reverse = s[::-1] ' 判断逆序排列后的字符串是否与原字符串相同 ' if s == s_reverse: return True else: return False
a, b = map(int, input().split())
' 循环判断a到b之间的整数是否为回文数 ' ans = set() # 使用集合存储答案,保证不重复 for i in range(a, b+1): if Ishuiwen(i): ans.add(i)
' 输出答案 ' for i in sorted(ans): print(i)
原文地址: http://www.cveoy.top/t/topic/n7Cz 著作权归作者所有。请勿转载和采集!