返回介绍

solution / 1500-1599 / 1518.Water Bottles / README_EN

发布于 2024-06-17 01:03:18 字数 3921 浏览 0 评论 0 收藏 0

1518. Water Bottles

中文文档

Description

There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return _the maximum number of water bottles you can drink_.

 

Example 1:

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:

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.

 

Constraints:

  • 1 <= numBottles <= 100
  • 2 <= numExchange <= 100

Solutions

Solution 1

class Solution:
  def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
    ans = numBottles
    while numBottles >= numExchange:
      numBottles -= numExchange - 1
      ans += 1
    return ans
class Solution {
  public int numWaterBottles(int numBottles, int numExchange) {
    int ans = numBottles;
    for (; numBottles >= numExchange; ++ans) {
      numBottles -= (numExchange - 1);
    }
    return ans;
  }
}
class Solution {
public:
  int numWaterBottles(int numBottles, int numExchange) {
    int ans = numBottles;
    for (; numBottles >= numExchange; ++ans) {
      numBottles -= (numExchange - 1);
    }
    return ans;
  }
};
func numWaterBottles(numBottles int, numExchange int) int {
  ans := numBottles
  for ; numBottles >= numExchange; ans++ {
    numBottles -= (numExchange - 1)
  }
  return ans
}
function numWaterBottles(numBottles: number, numExchange: number): number {
  let ans = numBottles;
  for (; numBottles >= numExchange; ++ans) {
    numBottles -= numExchange - 1;
  }
  return ans;
}
/**
 * @param {number} numBottles
 * @param {number} numExchange
 * @return {number}
 */
var numWaterBottles = function (numBottles, numExchange) {
  let ans = numBottles;
  for (; numBottles >= numExchange; ++ans) {
    numBottles -= numExchange - 1;
  }
  return ans;
};
class Solution {
  /**
   * @param Integer $numBottles
   * @param Integer $numExchange
   * @return Integer
   */
  function numWaterBottles($numBottles, $numExchange) {
    $ans = $numBottles;
    while ($numBottles >= $numExchange) {
      $numBottles = $numBottles - $numExchange + 1;
      $ans++;
    }
    return $ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文