给定两个整数N和E,其中N表示装满的水瓶的数量, E表示可以换成装满的水瓶的空瓶的数量。任务是找到可以清空的最大水瓶数量。
例子:
Input: N = 9, E = 3
Output: 13
Explanation:
Initially, there are 9 fully filled water bottles.
All of them are emptied to obtain 9 empty bottles. Therefore, count = 9
Then exchange 3 bottles at a time for 1 fully filled bottle.
Therefore, 3 fully filled bottles are obtained.
Then those 3 bottles are emptied to get 3 empty bottles. Therefore, count = 9 + 3 = 12
Then those 3 bottles are exchanged for 1 full bottle which is emptied.
Therefore, count = 9 + 3 + 1 = 13.
Input: N = 7, E = 5
Output: 8
Explanation:
Empty the 7 fully filled water bottles. Therefore, count = 7
Then exchange 5 bottles to obtain 1 fully filled bottle. Then empty that bottle.
Therefore, count = 7 + 1 = 8.
方法:必须按照以下步骤来解决问题:
- 将N的值存储在一个临时变量中,比如a。
- 初始化一个变量,比如s,用来存储空瓶子的数量,另一个变量,比如b,用来存储更换后剩余瓶子的数量。
- 现在,迭代直到a不等于0并将s增加a ,因为它将是已清空的最小瓶子数。
- 更新以下值:
- a到(a + b) / e
- b到N – (a * e)
- N到(a+b)
- 返回s作为必需的答案。
下面是上述方法的实现:
C++
// C++ program for the
// above approach
#include
using namespace std;
// Function to find the maximum
// bottles that can be emptied
int maxBottles(int n, int e)
{
int s = 0, b = 0;
int a = n;
// Iterate until a
// is non-zero
while (a != 0) {
// Add the number of
// bottles that are emptied
s = s + a;
// Update a after
// exchanging empty bottles
a = (a + b) / e;
// Stores the number of bottles
// left after the exchange
b = n - (a * e);
n = a + b;
}
// Return the answer
return s;
}
// Driver Code
int main()
{
int n = 9, e = 3;
// Function call
int s = maxBottles(n, e);
cout << s << endl;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function to find the maximum
// bottles that can be emptied
static int maxBottles(int n, int e)
{
int s = 0, b = 0;
int a = n;
// Iterate until a
// is non-zero
while (a != 0)
{
// Add the number of
// bottles that are emptied
s = s + a;
// Update a after
// exchanging empty bottles
a = (a + b) / e;
// Stores the number of bottles
// left after the exchange
b = n - (a * e);
n = a + b;
}
// Return the answer
return s;
}
// Driver Code
public static void main(String[] args)
{
int n = 9, e = 3;
// Function call
int s = maxBottles(n, e);
System.out.print(s + "\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the
# above approach
# Function to find the maximum
# bottles that can be emptied
def maxBottles(n, e):
s = 0
b = 0
a = n
# Iterate until a
# is non-zero
while (a != 0):
# Add the number of
# bottles that are emptied
s = s + a
# Update a after
# exchanging empty bottles
a = (a + b) // e
# Stores the number of bottles
# left after the exchange
b = n - (a * e)
n = a + b
# Return the answer
return s
# Driver Code
if __name__ == '__main__':
n = 9
e = 3
# Function call
s = maxBottles(n, e)
print(s)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum
// bottles that can be emptied
static int maxBottles(int n, int e)
{
int s = 0, b = 0;
int a = n;
// Iterate until a
// is non-zero
while (a != 0)
{
// Add the number of
// bottles that are emptied
s = s + a;
// Update a after
// exchanging empty bottles
a = (a + b) / e;
// Stores the number of bottles
// left after the exchange
b = n - (a * e);
n = a + b;
}
// Return the answer
return s;
}
// Driver Code
public static void Main()
{
int n = 9, e = 3;
// Function call
int s = maxBottles(n, e);
Console.Write(s + "\n");
}
}
// This code is contributed by code_hunt
Javascript
输出:
13
时间复杂度: O(N)
辅助空间: O(1)