题目来源:
leetcode题目,网址:1518. 换水问题 - 力扣(LeetCode)
解题思路:
按要求模拟计算即可。
解题代码:
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
int bottles=numBottles;
int sum=numBottles;
while(bottles>=numExchange){
int temp=bottles/numExchange;
bottles=bottles%numExchange;
bottles+=temp;
sum+=temp;
}
return sum;
}
}
总结:
官方题解给出了 两种解法。第一种是模拟。第二种基于数学,直接得到结果。