两位整数相乘形成的最大回文数是 9009 = 99 × 91。编写程序,求得任意输入的 n 位整数相乘形成的最大回文数。
题目内容:
两位整数相乘形成的最大回文数是 9009 = 99 × 91。编写程序,求得任意输入的 n 位整数相乘形成的最大回文数。
输入格式:
正整数 n
输出格式:
n 位整数相乘形成的最大回文数
输入样例:
2
输出样例:
9009
时间限制:1000ms内存限制:32000kb
def palin(num):
num_p = 0
num_t = num
while num_t != 0:
num_p = num_p * 10 + num_t % 10
num_t = num_t / 10
if num_p == num:
return True
else:
return False
def num_max(n):
x = 10 ** n - 1
return x
n = int(raw_input())
x = num_max(n)
y = num_max(n)
num = 0
count = 0
min_y = 0
while count != -1:
x -= 1
count += 1
num_t = x * y
if palin(num_t):
if num_t >= num:
num = num_t
min_y = x
if num_t <= num or num_t <= 10 ** (n*2-1) or count >= 10 ** 3:
num_t <= 10 ** (n*2-1)
y -= 1
x = y
count = 0
if y == min_y:
count = -1
print num
num_p = 0
num_t = num
while num_t != 0:
num_p = num_p * 10 + num_t % 10
num_t = num_t / 10
if num_p == num:
return True
else:
return False
def num_max(n):
x = 10 ** n - 1
return x
n = int(raw_input())
x = num_max(n)
y = num_max(n)
num = 0
count = 0
min_y = 0
while count != -1:
x -= 1
count += 1
num_t = x * y
if palin(num_t):
if num_t >= num:
num = num_t
min_y = x
if num_t <= num or num_t <= 10 ** (n*2-1) or count >= 10 ** 3:
num_t <= 10 ** (n*2-1)
y -= 1
x = y
count = 0
if y == min_y:
count = -1
print num