From 6a17490a797dd457b801af4eaf092b22d9177d23 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 10 Mar 2025 12:23:29 +0800 Subject: [PATCH] 20250310 finished. --- src/problem/mod.rs | 2 ++ .../p2269_find_the_k_beauty_of_a_number.rs | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/problem/p2269_find_the_k_beauty_of_a_number.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index a84ef23..b8e3bb7 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -528,3 +528,5 @@ mod p2597_the_number_of_beautiful_subsets; mod p2234_maximum_total_beauty_of_the_gardens; mod p2070_most_beautiful_item_for_each_query; + +mod p2269_find_the_k_beauty_of_a_number; diff --git a/src/problem/p2269_find_the_k_beauty_of_a_number.rs b/src/problem/p2269_find_the_k_beauty_of_a_number.rs new file mode 100644 index 0000000..68a5e66 --- /dev/null +++ b/src/problem/p2269_find_the_k_beauty_of_a_number.rs @@ -0,0 +1,36 @@ +/** + * [2269] Find the K-Beauty of a Number + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn divisor_substrings(num: i32, k: i32) -> i32 { + let k = k as usize; + let num_str: Vec = num.to_string().chars().collect(); + + (0..=num_str.len() - k) + .into_iter() + .map(|i| { + (&num_str[i..i + k]) + .iter() + .fold(0i32, |v, c| v * 10 + c.to_digit(10).unwrap() as i32) + }) + .filter(|&v| v != 0 && num % v == 0) + .count() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2269() { + assert_eq!(2, Solution::divisor_substrings(240, 2)); + assert_eq!(2, Solution::divisor_substrings(430043, 2)); + } +}