此代码旨在从2个3位数字中查找最大回文,但在最终回文之前就停止了puttign。请帮忙
def palindrome():
pdrome = -1
for num in range(100, 1000):
for num2 in range(100, 1000):
product = num * num2
sproduct = str(product)
length = len(sproduct)
if length % 2 == 0:
string1 = sproduct[0:length // 2]
string2 = sproduct[(length//2) + 1:]
else:
string1 = sproduct[0:(length//2)]
string2 = sproduct[((length//2) + 1):]
rstring = string2[::-1]
if string1 == rstring:
pdrome = product
print(pdrome)
palindrome()
如果我正确理解您的意图,您可以将内容重构为一个生成器,生成给定范围内所有可能的回文,然后使用
max()
获得最高回文:相关问题 更多 >
编程相关推荐