![[数论][枚举]leetcode479:最大回文数乘积,第1张 [数论][枚举]leetcode479:最大回文数乘积,第1张](/aiimages/%5B%E6%95%B0%E8%AE%BA%5D%5B%E6%9E%9A%E4%B8%BE%5Dleetcode479%EF%BC%9A%E6%9C%80%E5%A4%A7%E5%9B%9E%E6%96%87%E6%95%B0%E4%B9%98%E7%A7%AF.png)
题目:
题解:
思路:枚举
代码如下:
const int mod = 1337;
using LL = long long;
class Solution {
public:
int largestPalindrome(int n) {
if(n==1)return 9;
int upper=pow(10,n)-1;
// 从大到小地枚举回文数的左半部分
for(int left=upper;;--left){
LL p=left;
// 翻转左半部分到其自身末尾,构造回文数 p
for(int x=left;x>0;x/=10){
p=p*10+x%10;
}
for(LL x=upper;x*x>=p;x--){
if(p%x==0)// x是n位整数,且是p的因子,表明p就是最大回文整数
return p%mod;
}
}
return -1;
}
};
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)