码迷,mamicode.com
首页 > 其他好文 > 详细

欧拉项目004:寻找最大的回文数

时间:2014-05-22 18:48:46      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   tar   

Problem 4: Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

寻找有两个三位数相乘的最大回文数

我的python代码如下:

#method1:
def reverse(s):
    return s[::-1]
maxPalindromic = 0 
for i in range(100,999):
    for j in range(i,999):
        s = str(i*j)
        if s == reverse(s):
            if int(s)>maxPalindromic:
                maxPalindromic = int(s)
print maxPalindromic
#method2:
def reverse(s):
    return s[::-1]
def canBeDivide(num):
    for i in range(100,1000):
        if num%i==0 and num/i<1000:
            return True
    return False
def findMaxPalindromic():            
    for x in range(9,0,-1):
        for y in range(9,-1,-1):
            for z in range(9,-1,-1):
                num = 100001*x+10010*y+1100*z
                if canBeDivide(num):
                    return num
print findMaxPalindromic()
                    
                
方法一,比较直观,容易理解;

方法二,效率高。

有不懂得或者有更好的想法,可以留言,互相学习啊!


欧拉项目004:寻找最大的回文数,布布扣,bubuko.com

欧拉项目004:寻找最大的回文数

标签:style   class   blog   c   code   tar   

原文地址:http://blog.csdn.net/hackingwu/article/details/26503549

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!