项目euler pro3。在我的java代码中出了什么问题?我应该得到906609但我得到了580085

时间:2016-03-28 13:32:09

标签: java

/*
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.
*/


class Pro4
{
    int palindrome(int x)
    {
        int  n=x,rev=0;
        while(n!=0)
            {
                    rev=rev*10+n%10;
                    n=n/10;
              }
            if(x==rev)
                    return  x;
            else 
                    return 0;
         }
        public static void main(String args[])
        {
                int lar=0,i=0,j=0,x,k=100,l=100;
                Pro4 obj=new Pro4();
                for(i=100;i<=999;i++)
                        for(j=100;j<=999;j++)
                         {
                                x=obj.palindrome(i*j);
                            if(x!=0)
                            {
                                    lar=x;
                                    k=i;
                                    l=j;
                                }
                         }
                System.out.println(lar+","+k+","+l);    

        }
}

1 个答案:

答案 0 :(得分:0)

请在代码中进行以下更改

if(x != 0 && x>lar)
{
  lar=x;
  k=i;
  l=j;
}

输出

906609,913,993

更新:

到您之前的代码中,该代码只包含x!= 0条件,

906609回文变为i = 913,j = 993, 然后,下一个580085回文在i = 995, j = 583显示为两位数范围。

所以,一旦913,993(which gives 906609)处理答案得到了解,但湖泊处于适当状态,它将被995,583(which gives 580085)替换为什么tou得到了580085

相关问题