📌  相关文章
📜  从N个装满的瓶子中最大化空瓶子的数量

📅  最后修改于: 2021-04-23 18:40:42             🧑  作者: Mango

给定两个整数NE,其中N代表满水瓶的数量, E代表可以交换为满水瓶的空瓶的数量。任务是找到可以倒空的最大水瓶数量。

例子:

方法:必须遵循以下步骤来解决该问题:

  • N的值存储在一个临时变量中,例如a。
  • 初始化变量s来存储空瓶的数量,初始化另一个变量b来存储更换后剩余的瓶数。
  • 现在,迭代,直到一个不等于0,增量由A S,因为这将是已清空瓶的最小数量。
  • 更新以下值:
    • 从a(a + b)/ e
    • bN –(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)