-2
我是新来的Python(和编程),我停留在项目欧拉4.问题说:项目欧拉4蟒蛇:最大回文产品
“回文数读取相同的两种方式。由两个2位数字产品制成的最大回文是9009 = 91×99。
找到由两个3位数字产品制成的最大回文。
这里就是我来这么远:
ProductOfThree = []
ProductOfThreeSTR = []
PalindromicNumber = []
#This first for loop displays all the results possible from the product of two 3 digit Number
for k in range(100, 1000):
for j in range(k, 1000):
Result = k * j
ProductOfThree.append(Result)
#This second loop converts the list of number to a list of string
for i in ProductOfThree:
a = str(i)
ProductOfThreeSTR.append(a)
#The third loop compare the digit of each number of the list to find all the palindromic number of that list
for d in ProductOfThreeSTR:
if len(d) == 6:
if (d[0] == d[5]) and (d[1] == d[4]) and (d[2] == d[3]):
PalindromicNumber.append(d)
elif len(d) == 5:
if (d[0] == d[4]) and (d[1] == d[3]):
PalindromicNumber.append(d)
#And finally here the program display the largest number of the list, which contains only the palindromic numbers
Largest = PalindromicNumber[0]
for p in PalindromicNumber:
if Largest <= p:
Largest = p
print(Largest)
程序显示数99999。重新阅读程序后,我发现带有len(d)== 5的if语句是无用的,因为我们希望显示最大的数字,并且具有6位数字的数字总是大于5位数字。删除这部分程序后,我得到了我应该拥有的结果(906609)。但是我仍然想知道,即使我们试图找到5位数的回文数,当我们显示最大数目的列表时,通常它们应该被忽略,那么为什么它会给出99999的结果呢?