From 8d990172ab4b4f5515abea9e78c1bfa1b99b16ae Mon Sep 17 00:00:00 2001 From: wavty Date: Sat, 2 Sep 2023 00:37:28 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2240 No.2240.Number of Ways to Buy Pens and Pencils --- .../README.md | 14 ++++++++++++++ .../README_EN.md | 14 ++++++++++++++ .../Solution.rs | 9 +++++++++ 3 files changed, 37 insertions(+) create mode 100644 solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md index 61454afcf1db9..168799515e2ab 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README.md @@ -122,6 +122,20 @@ function waysToBuyPensPencils(total: number, cost1: number, cost2: number): numb } ``` +### **Rust** + +```rust +impl Solution { + pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { + let mut ans: i64 = 0; + for pen in 0..=total / cost1 { + ans += ((total - pen * cost1) / cost2) as i64 + 1; + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md index 5cd69d4b4aaf8..91a103ee0a5a9 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/README_EN.md @@ -108,6 +108,20 @@ function waysToBuyPensPencils(total: number, cost1: number, cost2: number): numb } ``` +### **Rust** + +```rust +impl Solution { + pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { + let mut ans: i64 = 0; + for pen in 0..=total / cost1 { + ans += ((total - pen * cost1) / cost2) as i64 + 1; + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs new file mode 100644 index 0000000000000..42eec8d80da9e --- /dev/null +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn ways_to_buy_pens_pencils(total: i32, cost1: i32, cost2: i32) -> i64 { + let mut ans: i64 = 0; + for pen in 0..=total / cost1 { + ans += ((total - pen * cost1) / cost2) as i64 + 1; + } + ans + } +} \ No newline at end of file