C++ ER_TICK=1000.0升/(双)时钟/秒; 常数时钟开始时间=时钟(); 长x,y; 常数长xy=euler4(&x,&y); 常数时钟结束时间=时钟(); printf(“在%f ms中找到%ld=%ld*%ld。\n”, xy,x,y,(结束时间

C++ ER_TICK=1000.0升/(双)时钟/秒; 常数时钟开始时间=时钟(); 长x,y; 常数长xy=euler4(&x,&y); 常数时钟结束时间=时钟(); printf(“在%f ms中找到%ld=%ld*%ld。\n”, xy,x,y,(结束时间,c++,C++,ER_TICK=1000.0升/(双)时钟/秒; 常数时钟开始时间=时钟(); 长x,y; 常数长xy=euler4(&x,&y); 常数时钟结束时间=时钟(); printf(“在%f ms中找到%ld=%ld*%ld。\n”, xy,x,y,(结束时间-开始时间)*毫秒每刻度 ); 返回退出成功; } 最好将数字转换为字符串,然后测试回文,回文速度比乘法和除法运算快得多。如果要查找最大回文,为什么要计数?如果将数字转换为string然后测试回文,回文比乘法和除法运算快得多。如果要查找最大的回

ER_TICK=1000.0升/(双)时钟/秒; 常数时钟开始时间=时钟(); 长x,y; 常数长xy=euler4(&x,&y); 常数时钟结束时间=时钟(); printf(“在%f ms中找到%ld=%ld*%ld。\n”, xy,x,y,(结束时间-开始时间)*毫秒每刻度 ); 返回退出成功; }
最好将
数字
转换为
字符串
,然后测试回文,回文速度比
乘法
除法
运算快得多。如果要查找最大回文,为什么要计数?如果将
数字
转换为
string
然后测试回文,回文比
乘法
除法
运算快得多。如果要查找最大的回文,为什么要进行计数?这是因为您已经对其进行了编码。删除行
cout谢谢。有没有一种方法可以在for循环之外识别a和b,这样我就可以显示到达输出所需的数字?只需创建变量来存储它们。无论何时更改最大值,都要更改它们,因为您已经对其进行了编码。删除行
cout谢谢。有没有一种方法可以在for循环之外识别a和b,这样我就可以显示到达输出所需的数字?只需创建变量来存储它们。无论什么时候改变,也要改变它们
   #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;
}
#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;
}
#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;
}