20250527 finished.

This commit is contained in:
jackfiled 2025-05-27 13:36:50 +08:00
parent 1cafc5eca5
commit 26bb74425e
2 changed files with 33 additions and 0 deletions

View File

@ -678,3 +678,5 @@ mod p2942_find_words_containing_character;
mod p2131_longest_palindrome_by_concatenating_two_letter_words;
mod p1857_largest_color_value_in_a_directed_graph;
mod p2894_divisible_and_non_divisible_sums_difference;

View File

@ -0,0 +1,31 @@
/**
* [2894] Divisible and Non-divisible Sums Difference
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn difference_of_sums(n: i32, m: i32) -> i32 {
(1..=n)
.filter_map(|x| if x % m != 0 { Some(x) } else { None })
.sum::<i32>()
- (1..=n)
.filter_map(|x| if x % m == 0 { Some(x) } else { None })
.sum::<i32>()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2894() {
assert_eq!(19, Solution::difference_of_sums(10, 3));
assert_eq!(15, Solution::difference_of_sums(5, 6));
assert_eq!(-15, Solution::difference_of_sums(5, 1));
}
}