📜  N卢比可购买的最大水量

📅  最后修改于: 2021-04-21 23:56:11             🧑  作者: Mango

给定N卢比。一升塑料瓶装水的成本A卢比和一升玻璃瓶装水的成本B卢比。但是购买后的空玻璃瓶可以换成C卢比。找到可以购买的最大公升水N卢比。

例子:

方法:如果我们至少有b钱,那么一个玻璃瓶的成本是b – c 。这意味着,如果a≤(b – c),那么我们就不需要购买玻璃瓶,而只需购买塑料瓶,答案将是floor(n / a) 。否则,我们需要尽力购买玻璃瓶。
所以,如果我们至少有b钱,那么我们将购买地板((n – c)/(b – c))玻璃瓶,然后将其余的钱花在塑料瓶上。

下面是上述方法的实现:

C++
// CPP implementation of the above approach
#include
using namespace std;
  
  
void maxLitres(int budget,int plastic,int glass,int refund)
{
  
    // if buying glass bottles is profitable
    if (glass - refund < plastic)
       {
           // Glass bottles that can be bought
        int ans = max((budget - refund) / (glass - refund), 0);
  
        // Change budget according the bought bottles
        budget -= ans * (glass - refund);
  
        // Plastic bottles that can be bought
        ans += budget / plastic;
        cout<


Java
// Java implementation of the above approach 
class GFG 
{
  
    static void maxLitres(int budget, int plastic, 
                            int glass, int refund)
    {
  
        // if buying glass bottles is profitable 
        if (glass - refund < plastic)
        {
              
            // Glass bottles that can be bought 
            int ans = Math.max((budget - refund) / (glass - refund), 0);
  
            // Change budget according the bought bottles 
            budget -= ans * (glass - refund);
  
            // Plastic bottles that can be bought 
            ans += budget / plastic;
            System.out.println(ans);
        } 
          
        // if only plastic bottles need to be bought 
        else 
        {
            System.out.println((budget / plastic));
        }
  
    }
  
    // Driver Code 
    public static void main(String[] args) 
    {
        int budget = 10, plastic = 11, glass = 9, refund = 8;
        maxLitres(budget, plastic, glass, refund);
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the above approach
  
def maxLitres(budget, plastic, glass, refund):
  
    # if buying glass bottles is profitable
    if glass - refund < plastic:
  
        # Glass bottles that can be bought
        ans = max((budget - refund) // (glass - refund), 0)
  
        # Change budget according the bought bottles
        budget -= ans * (glass - refund)
  
        # Plastic bottles that can be bought
        ans += budget // plastic
        print(ans)
  
    # if only plastic bottles need to be bought
    else:
        print(budget // plastic)
  
# Driver Code
budget, plastic, glass, refund = 10, 11, 9, 8
maxLitres(budget, plastic, glass, refund)


C#
// C# implementation of the above approach 
using System;    
  
class GFG 
{
  
    static void maxLitres(int budget, int plastic, 
                            int glass, int refund)
    {
  
        // if buying glass bottles is profitable 
        if (glass - refund < plastic)
        {
              
            // Glass bottles that can be bought 
            int ans = Math.Max((budget - refund) / (glass - refund), 0);
  
            // Change budget according the bought bottles 
            budget -= ans * (glass - refund);
  
            // Plastic bottles that can be bought 
            ans += budget / plastic;
            Console.WriteLine(ans);
        } 
          
        // if only plastic bottles need to be bought 
        else
        {
            Console.WriteLine((budget / plastic));
        }
  
    }
  
    // Driver Code 
    public static void Main(String[] args) 
    {
        int budget = 10, plastic = 11, glass = 9, refund = 8;
        maxLitres(budget, plastic, glass, refund);
    }
}
  
// This code contributed by Rajput-Ji


PHP


输出:
2