197 words
1 minutes
Netease Cloud Classroom Exam Questions - Python
Problem Statement:
The largest palindrome formed by the product of two 2-digit numbers is 9009 = 99 × 91. Write a program to find the largest palindrome formed by the product of two n-digit numbers, where n is an arbitrary input. Input Format: A positive integer n Output Format: The largest palindrome formed by the product of two n-digit numbers Sample Input: 2 Sample Output: 9009
Program:
def is_palindrome(num): #判断是否是回文
n = str(num)
if n == n[::-1]:
return True
else:
return False
n = int(input())
a = 1
arr1 = []
arr2 = []
while len(str(a)) < n + 1: #将n位数的数字加到数组中,同时抛弃n-1位数
if len(str(a)) > n - 1:
arr1.append(a)
arr2.append(a)
a = a + 1
maxPalindrome = 1
for i in arr1[::-1]: #从后面往前开始遍历,取得最大的回文数
for j in arr2[::-1]:
if i*j > maxPalindrome and is_palindrome(i*j):
maxPalindrome = i*j
print(maxPalindrome)Summary
I feel the algorithm is too brute-force and has low operational efficiency, but I haven’t thought of a better algorithm yet.
Netease Cloud Classroom Exam Questions - Python
https://blog.kisnows.com/en-US/2015/01/29/python-neteasy-exam/