欧拉项目第四题 Largest palindrome product

来源:互联网 发布:无锡汽车模具编程招聘 编辑:程序博客网 时间:2024/05/21 02:20

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

0 0