求两个3位数乘积的最大回文数
Euler #4 Find the largest palindrome made from the product of two 3-digit numbers
我想让我的程序只选择最大的产品,而不是所有的。附注:我知道有更有效的方法(比如取出副本),但我想这样做。
#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;
}
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- Usaco第1.6节主要回文
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 将 S1 转换为回文,并将 S2 作为其子字符串
- 数回文词
- 最长的回文子串(C++帮助)
- 有人可以详细解释这个回文代码是如何工作的吗?
- 回文递归版本
- 如何检查C ++ STL列表是否为回文?
- 回文数在 1 到 10000 之间
- C++ - 检查结构数据类型中的单词是否为回文
- 是回文作业练习
- 回文递归不停止
- 整数数组中最长的回文
- 回文测试
- 最大的回文产品(Project Euler)——C++
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 递归回文问题的时间复杂度,C++
- 程序以查找给定字符串中回文的子字符串的数量
- 最大回文数3位