[leetcode] 1518. Water Bottles

该博客介绍了一种模拟空瓶换水的过程,通过给定的满水瓶数和兑换规则,计算出能喝到的最大水瓶数。采用了一个while循环,当满水瓶数大于等于兑换数时,计算可以兑换的水瓶数,并更新总饮水数和剩余的空瓶数。最后返回最大饮水数。
摘要由CSDN通过智能技术生成

Description

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:

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.

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

分析

题目的意思是:给定numBottles个水瓶和numExchange,numExchange表示的是numExchange个空瓶可以兑换一个水瓶。如果弄懂了意思之后,就是一道智力题了,把这个过程模拟出来就可以了,我写了一个while循环来模拟这个过程了哈。

代码

class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        res=0
        while(numBottles>=numExchange):
            bottle=numBottles//numExchange
            t=bottle*numExchange
            res+=t
            numBottles-=t
            numBottles+=bottle
        return res+numBottles
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值