product=1
ans=[]
for i in range(100,999):
for j in range(100,999):
product=i*j
temp=product
while(product>0):
rev=1
dig=product%10
rev=rev*10+dig
product=product//10
if(temp==rev):
ans.append(rev)
print(ans)
谁可以告诉我在上述问题中有什么不对。我没有得到最终答案
Q)回文数字两种方式相同。由两个2位数字的乘积制成的最大回文是9009 = 91×99。
找到由两个3位数字的乘积制成的最大回文。
我认为你误解了我在你的代码中看到的问题。问题:回文数字的读取方式相同。由两个2位数字的乘积制成的最大回文是9009 = 91×99.所以在这种情况下回文是9009,因为当你向前和向后看时它仍然是9009.问题是找到两个3数字乘以它们会给你最大的回文数。
作为最大的回文可能是913 x 993 = 906609
所以你的代码将简化为此。
ans = 0
for i in range(100, 999):
for j in range(100, 999):
product = str(i * j)
if int(product) > ans: # check if the value is bigger then the last palindrome.
if product == product[::-1]: # check if it is a palindrome.
ans = int(product)
print(ans)
你从问题中我实际上会反转范围(1000,100,-1),因为我们发现最大值,因此最好向后工作。