From a3328f0d7fe7832bcb8057e189f26099297d8564 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Fri, 17 Dec 2021 10:45:20 +0800 Subject: [PATCH 1/3] add: leetcode 1518 solution --- leetcode/1518.Water-Bottles/1518.Water Bottles.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 leetcode/1518.Water-Bottles/1518.Water Bottles.go diff --git a/leetcode/1518.Water-Bottles/1518.Water Bottles.go b/leetcode/1518.Water-Bottles/1518.Water Bottles.go new file mode 100644 index 00000000..987c6b86 --- /dev/null +++ b/leetcode/1518.Water-Bottles/1518.Water Bottles.go @@ -0,0 +1,15 @@ +package leetcode + +func numWaterBottles(numBottles int, numExchange int) int { + if numBottles < numExchange { + return numBottles + } + quotient := numBottles / numExchange + reminder := numBottles % numExchange + ans := numBottles + quotient + for quotient+reminder >= numExchange { + quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange + ans += quotient + } + return ans +} From 7e9d5af03c16d32fd1cdcd46bcef95a177612911 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Fri, 17 Dec 2021 10:45:31 +0800 Subject: [PATCH 2/3] add: leetcode 1518 test --- .../1518.Water Bottles_test.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 leetcode/1518.Water-Bottles/1518.Water Bottles_test.go diff --git a/leetcode/1518.Water-Bottles/1518.Water Bottles_test.go b/leetcode/1518.Water-Bottles/1518.Water Bottles_test.go new file mode 100644 index 00000000..20c2a18d --- /dev/null +++ b/leetcode/1518.Water-Bottles/1518.Water Bottles_test.go @@ -0,0 +1,56 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1518 struct { + para1518 + ans1518 +} + +// para 是参数 +type para1518 struct { + numBottles int + numExchange int +} + +// ans 是答案 +type ans1518 struct { + ans int +} + +func Test_Problem1518(t *testing.T) { + + qs := []question1518{ + + { + para1518{9, 3}, + ans1518{13}, + }, + + { + para1518{15, 4}, + ans1518{19}, + }, + + { + para1518{5, 5}, + ans1518{6}, + }, + + { + para1518{2, 3}, + ans1518{2}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1518------------------------\n") + + for _, q := range qs { + _, p := q.ans1518, q.para1518 + fmt.Printf("【input】:%v 【output】:%v\n", p, numWaterBottles(p.numBottles, p.numExchange)) + } + fmt.Printf("\n\n\n") +} From 70740cc973fc8207d815ac14cbc409288c3521b8 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Fri, 17 Dec 2021 10:45:44 +0800 Subject: [PATCH 3/3] add: leetcode 1518 readme --- leetcode/1518.Water-Bottles/README.md | 75 +++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 leetcode/1518.Water-Bottles/README.md diff --git a/leetcode/1518.Water-Bottles/README.md b/leetcode/1518.Water-Bottles/README.md new file mode 100644 index 00000000..ebbfb401 --- /dev/null +++ b/leetcode/1518.Water-Bottles/README.md @@ -0,0 +1,75 @@ +# [1518. Water Bottles](https://leetcode-cn.com/problems/water-bottles/) + +## 题目 + +Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle. + +The operation of drinking a full water bottle turns it into an empty bottle. + +Return the maximum number of water bottles you can drink. + +**Example 1**: + +![https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png) + + Input: numBottles = 9, numExchange = 3 + Output: 13 + Explanation: You can exchange 3 empty bottles to get 1 full water bottle. + Number of water bottles you can drink: 9 + 3 + 1 = 13. + +**Example 2**: + +![https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png) + + Input: numBottles = 15, numExchange = 4 + Output: 19 + Explanation: You can exchange 4 empty bottles to get 1 full water bottle. + Number of water bottles you can drink: 15 + 3 + 1 = 19. + +**Example 3**: + + Input: numBottles = 5, numExchange = 5 + Output: 6 + +**Example 4**: + + Input: numBottles = 2, numExchange = 3 + Output: 2 + +**Constraints:** + +- 1 <= numBottles <= 100 +- 2 <= numExchange <= 100 + +## 题目大意 + +小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。 + +如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。 + +请你计算 最多 能喝到多少瓶酒。 + +## 解题思路 + +- 模拟 +首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒,然后再喝完这瓶酒,得到一个空瓶。这样模拟下去,直到所有的空瓶子小于numExchange结束 + +## 代码 + +```go +package leetcode + +func numWaterBottles(numBottles int, numExchange int) int { + if numBottles < numExchange { + return numBottles + } + quotient := numBottles / numExchange + reminder := numBottles % numExchange + ans := numBottles + quotient + for quotient+reminder >= numExchange { + quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange + ans += quotient + } + return ans +} +``` \ No newline at end of file