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.

解法

1.

题意就是让你找一个最大回文数 ,是由两个三位数乘积可得到的。

(这编程题嘛~)

我们这里采用了两个函数,一个是$itoa$函数, 另一个是$strrev$,两个都是非标准$C/C++$库中的函数。

  • $itoa$:一个把整数转换成为字符串的函数。

    在头文件$cstdlib$中 用法如下:

  • $strrev$:将字符串中的字符按照逆序进行反转。

    在头文件$cstring$中,用法如下:

注意:此处判断的时候 一定要遍历字符串判断 不然会出现$5675->5765$这种情况 !

这样我们从999开始 对$i j$ 进行$i * j$判断是不是回文数,如果是最大的,输出即可

(我们这里用两个参数$ans$和$solve$保存一下,其中需要记录当前最大的回文数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
#define ll long long
#define IOS {ios::sync_with_stdio(false);cin.tie(0);}
using namespace std;
//10000--998001
int ans, solve = 0;
bool is_palindrome_number(int x)
{
char temp[10], str[10];
itoa(x, str, 10);
itoa(x, temp, 10);
strrev(temp);
//cout << temp << endl << str << endl;
int len = strlen(str);
for(int i = 0; i <= len; i++)
{
if(temp[i] != str[i])
{
return false;
}
}
return true;
}
int main(void)
{
IOS
for(int i = 999; i >= 100; i--)
{
for(int j = 999; j >= 100; j--)
{
ans = i * j;
if(is_palindrome_number(ans) && ans > solve)
{
solve = ans;
}
}
}
cout << solve << endl;
return 0;
}

2.

数学方法就是用取模去判断三位数是不是一样的

可参考:https://www.cnblogs.com/zhouyinhui/archive/2011/01/14/1935824.html

正确答案

官方解答