0
点赞
收藏
分享

微信扫一扫

欧拉计划第4题题解

Largest palindrome product

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.

最大回文乘积

回文数就是从前往后和从后往前读都一样的数。由两个2位数相乘得到的最大回文乘积是 9009 = 91 × 99。

找出由两个3位数相乘得到的最大回文乘积。

解题思路

这道题目没有什么好的思路。想到的办法就是枚举所有的三位数,求它们的乘积,然后在所有的回文乘积里找最大值就是我们的答案。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int ans;
bool check(int a) {
int x = a, b = 0;
while (x) {
b = b * 10 + x % 10;
x /= 10;
}
return a == b;
}
int main() {
for (int i = 100; i < 1000; i ++)
for (int j = i; j < 1000; j ++)
if (check(i*j))
ans = max(ans, i*j);
cout << ans << endl;
return 0;
}

答案是 \(906609\)。


举报

相关推荐

0 条评论