projectEuler pro4

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.

 

JAVA:

public class P004 {
public static void main(String[] args){
int a =0;
int out=0;
for(int i=1;i<1000;i++){
for(int j=1;j<1000;j++){
a = i*j;
int[] d = bignum(a);
if(isPalindromic(d))
out = Math.max(a, out);
}
}
System.out.print(out);
}
public static int[] bignum(int a){ //提取每位数字
int[] d = new int[6];
int b = 100000;
for(int i=0;i<6;i++){
d[i] = a/outb(b,i);
a = a- a/outb(b,i)*outb(b,i);
}
int length = 0;
for(int i = 0;i<d.length;i++){ //此for循环为了判断数长度
if(d[i]==0)
length+=1;
else
break;
}
length = d.length-length;
int[] e = new int[length];
for(int i=0;i<e.length;i++){
e[i] = d[d.length-e.length+i];
}
return e;
}
public static int outb(int b,int i){
for(int a=1; a<=i;a++)
b=b/10;
return b;
}
public static boolean isPalindromic(int[] d){
if(d.length ==1)
return true;
else
for(int i=0;i<d.length/2+1;i++){
if(d[i]!=d[d.length-i-1])
return false;
}
return true;
}
}

posted on 2013-10-09 21:32  Monster_Ch  阅读(125)  评论(0编辑  收藏  举报

导航