Largest palindrome product
Problem 4
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.
最大的回文数
问题 4
一个回文数从两边看是一样的。由两个两位数相乘获得的最大的回文数是 9009 = 91 99.
求由两个3位数相乘获得的最大的回文数.
public class Euler4
{
//判断整数num是否为回文数
public static boolean isPal(int num)
{
String str=num+"";int len=str.length();
for(int i=0;i<=len/2;i++)
{
if(str.substring(i,i+1).equals(str.substring(len-i-1,len-i))!=true)
return false;
}
return true;
}
public static void main(String[] args)
{
int num=0,numMax=0,iCopy=100,jCopy=100;
for(int i=100;i<1000;i++)
{
for(int j=100;j<1000;j++)
{
num=i*j;
if(Euler4.isPal(num)==true)
{
iCopy=i;jCopy=j;
numMax=numMax>num? numMax:num;
System.out.println("num="+i+"×"+j+"="+num);
}
}
}
System.out.println("numMax="+iCopy+"×"+jCopy+"="+numMax);
}
}
转载于:https://blog.51cto.com/qianke/1391148