标签:log amp could 检查 html note targe col inpu
问题描述:
Find the largest palindrome made from the product of two n-digit numbers.
Since the result could be very large, you should return the largest palindrome mod 1337.
Example:
Input: 2
Output: 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
Note:
The range of n is [1,8].
解题思路:
没有思路,参考了GrandYang的解法
先找n个数字能够组成的最大值upper和最小值lower
最大的值的长度应为2n
我们可以从上界upper将其做镜面反射构成一个回文然后检查是否存在两个值可以得到这样的乘积。
代码:
class Solution { public: int largestPalindrome(int n) { int upper = pow(10, n) - 1, lower = upper/10; for(int i = upper; i > lower; i--){ string s = to_string(i); long p = stol(s + string(s.rbegin(), s.rend())); for(long j = upper; j*j > p; --j){ if(p % j == 0) return p % 1337; } } return 9; } };
479. Largest Palindrome Product
标签:log amp could 检查 html note targe col inpu
原文地址:https://www.cnblogs.com/yaoyudadudu/p/9222521.html