Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92c1af9

Browse files
Bruce YangBruce Yang
Bruce Yang
authored and
Bruce Yang
committed
Add C++ solution for leetcode 1518.
1 parent 374ff72 commit 92c1af9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int numWaterBottles(int bot, int exc) { /* bot: 一开始酒的瓶数, exc: 换一瓶新酒需要的空瓶数 */
9+
int res = bot;
10+
while (bot >= exc)
11+
{
12+
res += bot/exc; // 当前次循环可以新换来的酒的瓶数
13+
bot = bot/exc + bot % exc; /* 更新总的酒瓶数: 新换来的酒喝完后就成了空瓶, 与当前没用完的酒瓶放在一起, 为下一轮循环做准备 */
14+
}
15+
return res;
16+
}
17+
};
18+
19+
// Test
20+
int main()
21+
{
22+
Solution sol;
23+
int bot = 24;
24+
int exc = 4;
25+
auto res = sol.numWaterBottles(bot, exc);
26+
cout << res << endl;
27+
28+
return 0;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /