求两个3位数乘积的最大回文数

Euler #4 Find the largest palindrome made from the product of two 3-digit numbers

本文关键字:回文 3位 两个      更新时间:2023-10-16

我想让我的程序只选择最大的产品,而不是所有的。附注:我知道有更有效的方法(比如取出副本),但我想这样做。

   #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;
}

您可以创建一个变量来存储其中最大的回文。并根据它检查每个回文。

例如:

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

if语句将放在"if (ispal(pal))"里面,并且您应该在程序的顶部创建最大。在程序结束时,您可以显示最大以查看答案。

完整代码:

#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;
}

我用不同的语言解决过几次这个问题。这是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;
}