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.
물병(물이 차있는 병)과 빈병이 있다. 함수의 첫째 인자인 numBottles 는 물병개수 이고 numExchange 는 교환개수 이다. 교환개수란 예를들어 교환개수가 3이면 3개의 빈병으로 하나의 물병을 바꿀 수 있다는 의미이다. 물병을 마시면 빈병이 된다. 물병을 몇개나 마실 수 있는가?
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.
9개의 물병을 마시면 9빈병이 된다. 3대1로 교환하면 3개의 물병을 얻는다. 3개의 물병을 마시면 3개의 빈병이 된다. 3개의 빈병을 3대1로 교환하면 1개의 물병을 얻는다. 1개의 물병을 마신다.
총 9 + 3 + 1 = 13 개의 물병을 마셨으므로 13을 반환한다
public int numWaterBottles(int numBottles, int numExchange)
{
int ret = numBottles;//drink
int empty = 0;
int full = 0;
while(numBottles >= numExchange)
{
full = numBottles / numExchange;
empty = numBottles % numExchange;
//Drink
ret += full;
empty += full;
numBottles = empty;
}
return ret;
}