📌  相关文章
📜  可以用 N 卢比购买的最大升水量

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

给定的N 卢比。一升塑料瓶水的成本A 卢比和一升玻璃瓶水的费用B 卢比。但是购买后的空玻璃瓶可以换C 卢比。找出可以购买的最大升水量N 卢比。
例子:

方法:如果我们至少有b 钱那么一个玻璃瓶的成本是b – c 。这意味着如果a ≤ (b – c)那么我们不需要买玻璃瓶,只买塑料瓶,答案是floor(n / a) 。否则,我们需要尽可能购买玻璃瓶。
所以,如果我们至少有b 钱,那么我们将购买floor((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


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程