-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
50 lines (45 loc) · 1.47 KB
/
main.rs
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
41
42
43
44
45
46
47
48
49
50
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
/// 回文数p可表示为p=(10^n-i)*(10^n-j) = (10^n-i-j)*10^n + i*j
/// 令 upper = 10^n-i-j, lower = i*j, 两者形成回文的关系
/// 只要使得i+j尽可能小,回文数p便可以尽可能大,枚举之
pub fn largest_palindrome(n: i32) -> i32 {
if n == 1 { return 9 }
let top = u64::pow(10, n as u32);
let mut sum = 1;
loop {
let upper = top - sum;
let lower = upper.to_string().chars().rev().collect::<String>().parse().unwrap();
let mut i = sum >> 1;
let mut j = sum - i;
loop {
if i * j < lower { break }
if i * j == lower {
return ((upper * top + lower) % 1337) as i32
}
i -= 1;
j += 1;
}
sum += 1
}
}
}
#[cfg(test)]
mod test {
use crate::*;
use crate::Solution;
#[test]
fn basic() {
assert_eq!(Solution::largest_palindrome(1),9);
assert_eq!(Solution::largest_palindrome(2),987);
assert_eq!(Solution::largest_palindrome(3),123);
assert_eq!(Solution::largest_palindrome(4),597);
assert_eq!(Solution::largest_palindrome(5),677);
assert_eq!(Solution::largest_palindrome(6),1218);
assert_eq!(Solution::largest_palindrome(7),877);
assert_eq!(Solution::largest_palindrome(8),475);
}
}