Skip to content

Commit c99d3ef

Browse files
authored
Merge pull request halfrost#214 from gostool/leetcode1518
Leetcode1518
2 parents 7930b8d + 70740cc commit c99d3ef

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode
2+
3+
func numWaterBottles(numBottles int, numExchange int) int {
4+
if numBottles < numExchange {
5+
return numBottles
6+
}
7+
quotient := numBottles / numExchange
8+
reminder := numBottles % numExchange
9+
ans := numBottles + quotient
10+
for quotient+reminder >= numExchange {
11+
quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange
12+
ans += quotient
13+
}
14+
return ans
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1518 struct {
9+
para1518
10+
ans1518
11+
}
12+
13+
// para 是参数
14+
type para1518 struct {
15+
numBottles int
16+
numExchange int
17+
}
18+
19+
// ans 是答案
20+
type ans1518 struct {
21+
ans int
22+
}
23+
24+
func Test_Problem1518(t *testing.T) {
25+
26+
qs := []question1518{
27+
28+
{
29+
para1518{9, 3},
30+
ans1518{13},
31+
},
32+
33+
{
34+
para1518{15, 4},
35+
ans1518{19},
36+
},
37+
38+
{
39+
para1518{5, 5},
40+
ans1518{6},
41+
},
42+
43+
{
44+
para1518{2, 3},
45+
ans1518{2},
46+
},
47+
}
48+
49+
fmt.Printf("------------------------Leetcode Problem 1518------------------------\n")
50+
51+
for _, q := range qs {
52+
_, p := q.ans1518, q.para1518
53+
fmt.Printf("【input】:%v 【output】:%v\n", p, numWaterBottles(p.numBottles, p.numExchange))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}

leetcode/1518.Water-Bottles/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1518. Water Bottles](https://leetcode-cn.com/problems/water-bottles/)
2+
3+
## 题目
4+
5+
Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.
6+
7+
The operation of drinking a full water bottle turns it into an empty bottle.
8+
9+
Return the maximum number of water bottles you can drink.
10+
11+
**Example 1**:
12+
13+
![https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png)
14+
15+
Input: numBottles = 9, numExchange = 3
16+
Output: 13
17+
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
18+
Number of water bottles you can drink: 9 + 3 + 1 = 13.
19+
20+
**Example 2**:
21+
22+
![https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png](https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png)
23+
24+
Input: numBottles = 15, numExchange = 4
25+
Output: 19
26+
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
27+
Number of water bottles you can drink: 15 + 3 + 1 = 19.
28+
29+
**Example 3**:
30+
31+
Input: numBottles = 5, numExchange = 5
32+
Output: 6
33+
34+
**Example 4**:
35+
36+
Input: numBottles = 2, numExchange = 3
37+
Output: 2
38+
39+
**Constraints:**
40+
41+
- 1 <= numBottles <= 100
42+
- 2 <= numExchange <= 100
43+
44+
## 题目大意
45+
46+
小区便利店正在促销,用 numExchange 个空酒瓶可以兑换一瓶新酒。你购入了 numBottles 瓶酒。
47+
48+
如果喝掉了酒瓶中的酒,那么酒瓶就会变成空的。
49+
50+
请你计算 最多 能喝到多少瓶酒。
51+
52+
## 解题思路
53+
54+
- 模拟
55+
首先我们一定可以喝到 numBottles 瓶酒,剩下 numBottles 个空瓶。接下来我们可以拿空瓶子换酒,每次拿出 numExchange 个瓶子换一瓶酒,然后再喝完这瓶酒,得到一个空瓶。这样模拟下去,直到所有的空瓶子小于numExchange结束
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func numWaterBottles(numBottles int, numExchange int) int {
63+
if numBottles < numExchange {
64+
return numBottles
65+
}
66+
quotient := numBottles / numExchange
67+
reminder := numBottles % numExchange
68+
ans := numBottles + quotient
69+
for quotient+reminder >= numExchange {
70+
quotient, reminder = (quotient+reminder)/numExchange, (quotient+reminder)%numExchange
71+
ans += quotient
72+
}
73+
return ans
74+
}
75+
```

0 commit comments

Comments
 (0)