📜  从 N 个装满的瓶子中最大化空水瓶的数量

📅  最后修改于: 2021-10-26 06:27:19             🧑  作者: Mango

给定两个整数NE,其中N表示装满的水瓶的数量, E表示可以换成装满的水瓶的空瓶的数量。任务是找到可以清空的最大水瓶数量。

例子:

方法:必须按照以下步骤来解决问题:

  • N的值存储在一个临时变量中,比如a。
  • 初始化一个变量,比如s,用来存储空瓶子的数量,另一个变量,比如b,用来存储更换后剩余瓶子的数量。
  • 现在,迭代直到a不等于0并将s增加a ,因为它将是已清空的最小瓶子数。
  • 更新以下值:
    • 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)