solving project euler #4 with c++

时间:2015-06-30 19:21:54

标签: c++

Q.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.

this is my code .it worked for 2 digit problem and gave 9009 as answer but it is not working for 3 digit number part.

#include <iostream>
using namespace std ;
bool ispallin(int n)
{
int  reverse = 0, temp;
temp = n;

 while (temp != 0)
 {
  reverse = reverse * 10;
  reverse = reverse + temp%10;
  temp    = temp/10;
  }

  if (n == reverse)
  return true;
  else
  return false;


}
 int main() {
int a[999*999]={0},i,j,no;//0 implies no. at i th position is pallindrome
for(i=100;i<1000;i++)
{
for(j=100;j<1000;j++)
{
    if(a[i*j]==0)//no. is pallin
    {
         if(ispallin(i*j))
         no=i*j;

        else a[i*j]=1;//no. at this pos is not pallindrome
}
 }

}
cout<<no;
return 0;
}

1 个答案:

答案 0 :(得分:1)

你的代码非常好,我会为函数添加更多的注释或有意义的名称,但它是很好的代码,答案不是你想要的,因为你从来没有确定结果是有最高的。 也许700 * 700会是一个回文,但是701 * 600也是回文,我的情况一旦'我'更大它会覆盖结果,即使polindrom是一个更大的。