Euler#4找到由两个3位数字的乘积制成的最大回文

时间:2015-10-01 23:52:55

标签: c++

我试图让我的程序只选择最大的产品而不是所有产品。 P.S我知道有更有效的方法可以做到这一点(例如取出重复项),但我想这样做。

   #include <iostream>

using namespace std;

bool isPal(int);

int main()
{
    int pal;
    // Finds largest product
    for (int a = 100; a < 1000; a++)
    {
        for (int b = 100; b < 1000; b++)
        {
            pal = a * b;
            if (isPal(pal))
            {
                cout << pal << "(" << a << "*" << b << ")" << endl;
            }
        }
    }
    system("pause");
    return 0;
}


bool isPal(int num)
{
    bool status = true;
    int digit, rev = 0, ck_num; // Added new variable
    ck_num = num; // Assigned it to variable num

    // Tests for palindrome
    while (num)
    {
        digit = num % 10;
        num /= 10;
        rev = rev * 10 + digit;
    }

    if (rev == ck_num) // Checked it against unchanged variable
        status = true;
    else
        status = false;
    return status;

}

2 个答案:

答案 0 :(得分:0)

您可以创建一个存储最大回文的变量。并检查每个回文。

例如:

int largest = 0;

if (pal > largest) {
    largest = pal;
}

if语句将进入&#34; if(ispal(pal))&#34;你应该在程序的顶部创建最大的。 在程序结束时,您可以显示最大值以查看答案。

完整代码:

#include <iostream>

using namespace std;

bool isPal(int);

int main()
{
    int largest;
    int pal;
    // Finds largest product
    for (int a = 100; a < 1000; a++)
    {
        for (int b = 100; b < 1000; b++)
        {
            pal = a * b;
            if (isPal(pal))
            {
                if (pal > largest) {
                    largest = pal;
                }
            }
        }
    }

    cout << "Answer: " << largest << endl;
    system("pause");
    return 0;
}

答案 1 :(得分:0)

我用不同的语言几次解决了这个问题。这是我的C解决方案。从一些基本数论得到一个很大的加速,这是一个有趣的。我可能不是第一个提出这种算法的人,但我独立想出了它。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

long euler4( long* const px, long* const py )
/* Finds the largest xy such that xy is a pal-
 * indrome, x and y are three-digit numbers,
 * and x*y = xy.
 */
{
/* We begin by enumerating the 900 possible
 * six-digit palindromes.
 */
  for ( long i = 9; i > 0; --i )
    for ( long j = 9; j >= 0; --j )
      for ( long k = 9; k >= 0; --k ) {
/* Any six-digit palindrome has the form
 * 100001i + 010010j + 001100k =
 *   11*(9091i+910j+100k)
 * Since 11 is prime, it must be a factor of
 * x or y.  Without loss of generality, let us
 * make it x.  We know that x must be at least
 * xy/999, at least 100, at most xy/100, at most
 * 999, and a multiple of 11.
 *
 * We could prune some more--for instance, xy
 * cannot be divisible by any prime between
 * 1000 and 999999/11=90909 if it is the
 * product of two three-digit factors--but I
 * don't see a way to improve performance by
 * doing so.
 */
        const long xy = 100001L*i+10010*j+1100*k;
        long x = xy/999+10;
        x = x - (x%11);
        x = (x > 110L) ? x : 110L;
        for ( ; x < 1000; x += 11 ) {
          const long y = xy/x;
          if ( y < 100 )
            break;
          if ( x*y == xy ) {
            *px = x;
            *py = y;
            return xy;
          } // end if
        } // end for x
      } // end for k

  fflush(stdout);
  fprintf( stderr, "No six-digit palindrome found.\n" );
  exit(EXIT_FAILURE);
  return -1; // Not reached.
}

int main(void) {
  static const double MS_PER_TICK = 1000.0L / (double)CLOCKS_PER_SEC;
  const clock_t start_time = clock();

  long x, y;
  const long xy = euler4(&x, &y);

  const clock_t end_time = clock();
  printf("Found %ld=%ld*%ld in %f ms.\n",
         xy, x, y, (end_time-start_time)*MS_PER_TICK
        );
  return EXIT_SUCCESS;
}