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
program main
implicit none
integer :: numBottles, numExchange, maxWaterBottles
numBottles = 9
numExchange = 3
write (*, '(A, I0)') 'Example 1: ', maxWaterBottles(numBottles, numExchange)
numBottles = 15
numExchange = 4
write (*, '(A, I0)') 'Example 2: ', maxWaterBottles(numBottles, numExchange)
contains
function maxWaterBottles(n, m) result(res)
implicit none
integer, value :: n, m
integer :: res
res = n + m - 1
end function maxWaterBottles
end program main
temp.f95:16:29: 16 | function maxWaterBottles(n, m) result(res) | 1 Error: Procedure ‘maxwaterbottles’ at (1) has an explicit interface from a previous declaration temp.f95:17:21: 17 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:18:30: 18 | integer, value :: n, m | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:19:22: 19 | integer :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:23: 21 | res = n + m - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:7: 22 | end function maxWaterBottles | 1 Error: Expecting END PROGRAM statement at (1)
module WaterBottles
implicit none
contains
function maxWaterBottles(numBottles, numExchange) result(maxBottles)
integer, intent(in) :: numBottles
integer, intent(in) :: numExchange
integer :: maxBottles
integer :: fullBottles
integer :: emptyBottles
fullBottles = numBottles / numExchange
emptyBottles = numBottles - (fullBottles * numExchange)
maxBottles = fullBottles + emptyBottles
end function maxWaterBottles
end module WaterBottles
program main
use WaterBottles
implicit none
integer :: numBottles
integer :: numExchange
integer :: maxBottles
! Example 1
numBottles = 9
numExchange = 3
maxBottles = maxWaterBottles(numBottles, numExchange)
write (*,*) "Example 1:", maxBottles
! Example 2
numBottles = 15
numExchange = 4
maxBottles = maxWaterBottles(numBottles, numExchange)
write (*,*) "Example 2:", maxBottles
end program main
Example 1: 3 Example 2: 6
def numWaterBottles(numBottles: int, numExchange: int) -> int:
totalBottles = numBottles
while numBottles >= numExchange:
newBottles = numBottles // numExchange
totalBottles += newBottles
numBottles = newBottles + numBottles % numExchange
return totalBottles
The algorithm initializes the totalBottles variable with the value of numBottles. It then uses a while loop to repeatedly calculate the number of new full bottles the person can get by exchanging the current number of empty bottles.
In each iteration, the loop calculates the new number of full bottles by dividing numBottles by numExchange and flooring the result. It then adds these new bottles to the totalBottles variable. The loop also updates the number of empty bottles by adding the new full bottles and the remainder of numBottles divided by numExchange.
Once the number of empty bottles is less than the required numExchange, the loop terminates, and the function returns the value of totalBottles as the maximum number of water bottles the person can drink.
int numWaterBottles(int numBottles, int numExchange) {
int totalBottles = numBottles;
while (numBottles >= numExchange) {
int newBottles = numBottles / numExchange;
totalBottles += newBottles;
numBottles = newBottles + numBottles % numExchange;
}
return totalBottles;
}
The algorithm initializes the totalBottles variable with the value of numBottles. It then uses a while loop to repeatedly calculate the number of new full bottles the person can get by exchanging the current number of empty bottles.
In each iteration, the loop calculates the new number of full bottles by dividing numBottles by numExchange and flooring the result. It then adds these new bottles to the totalBottles variable. The loop also updates the number of empty bottles by adding the new full bottles and the remainder of numBottles divided by numExchange.
Once the number of empty bottles is less than the required numExchange, the loop terminates, and the function returns the value of totalBottles as the maximum number of water bottles the person can drink.