Leetcode 1518 换酒问题

 

  JAVA:

public final int numWaterBottles(int numBottles, int numExchange) {
        int re = numBottles, empty = numBottles;
        while (empty >= numExchange) {
            int newBeer = empty / numExchange;
            re += newBeer;
            empty = empty % numExchange + newBeer;
        }
        return re;
    }

  JS:

/**
 * @param {number} numBottles
 * @param {number} numExchange
 * @return {number}
 */
var numWaterBottles = function (numBottles, numExchange) {
    let re = numBottles, empty = numBottles;
    while (empty >= numExchange) {
        let newBeer = Math.floor(empty / numExchange);
        re += newBeer;
        empty = empty % numExchange + newBeer;
    }
    return re;
};

 

posted @ 2021-05-09 22:09  牛有肉  阅读(65)  评论(0编辑  收藏  举报