您的位置:首页 > 其它

欧拉项目第四题 Largest palindrome product

2016-03-09 09:36 357 查看
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.
回文数 从左往右写 与 从右往左写是一样的,两个两位数的乘积是回文数的最大值是9009=91*99
求两个三位数乘机是回文数的最大值
其实就是求 10000-998001之间、可由两个三位数的乘机的最大回文数
我的想法是,列举回文数,一个个暴力破解
假设6位回文数是xyzzyx,x是1-9,y、z是0-9,5位的暂不考虑
public static void main(String[] args) {
       for(int x = 9;x>=1;x--){
           for(int y = 9;y>=0;y--){
               for(int z = 9;z>=0;z--){
                   int hw = x * 100000 + y * 10000 + z* 1000 + z * 100 + y * 10 + x;
                   if(check(hw)) {
                       System.out.println(hw);
                       System.exit(0);
                   }
                   
               }
           }
       }
    }
   
    public static Boolean check(int num){
        for(int i=100;i<Math.sqrt(num);i++){
            if(num%i==0 && num/i >= 100 && num/i <= 999) {
                System.out.println(num + "=" + i + "*" + num/i);
                return true;
            }
        }
        return false;
    }
结果是
906609=913*993
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: