From 8830b0dbcd530b6d3088e876d5fe2f0507e1931b Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 26 Sep 2024 13:26:35 +0800 Subject: [PATCH] 20240926 finished. --- src/problem/mod.rs | 3 +- ...n_element_sum_and_digit_sum_of_an_array.rs | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2535_difference_between_element_sum_and_digit_sum_of_an_array.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index cdcb39c..4b27568 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -245,4 +245,5 @@ mod p2376_count_special_integers; mod p2374_node_with_highest_edge_score; mod p997_find_the_town_judge; mod p2207_maximize_number_of_subsequences_in_a_string; -mod p2306_naming_a_company; \ No newline at end of file +mod p2306_naming_a_company; +mod p2535_difference_between_element_sum_and_digit_sum_of_an_array; \ No newline at end of file diff --git a/src/problem/p2535_difference_between_element_sum_and_digit_sum_of_an_array.rs b/src/problem/p2535_difference_between_element_sum_and_digit_sum_of_an_array.rs new file mode 100644 index 0000000..44d3d37 --- /dev/null +++ b/src/problem/p2535_difference_between_element_sum_and_digit_sum_of_an_array.rs @@ -0,0 +1,42 @@ +/** + * [2535] Difference Between Element Sum and Digit Sum of an Array + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn difference_of_sum(nums: Vec) -> i32 { + let mut result = 0; + + for num in nums { + let mut num = num; + let mut base = 1; + + while num > 0 { + let digit = num % 10; + + result += digit * base - digit; + + base *= 10; + num = num / 10; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2535() { + assert_eq!(9, Solution::difference_of_sum(vec![1, 15, 6, 3])); + assert_eq!(0, Solution::difference_of_sum(vec![1, 2, 3, 4])); + } +}