📜  等量行李的最少数量,至少可收集M笔钱

📅  最后修改于: 2021-04-24 16:46:44             🧑  作者: Mango

给定无限数量的两种面额XY的硬币。还赠送了容量为N卢比的袋子,与硬币数量无关。任务是找到最小数量的袋子,以使每个袋子包含相同数量的卢比,并且所有袋子的总和至少为M。
例子 :

Input : M = 27, N = 12, X = 2, Y = 5. 
Output : 3
We put 2 coins of X, 1 coin of Y in each bag. 
So we have 9 rupees in each bag and we need 
at least 3 bags (Note that 27/9 = 3). There 
is no way to obtain sum with lesser number
of bags.

Input : M = 45, N = 9, X = 4, Y = 5. 
Output : 5

任务是最小化袋子的数量,因此需要最大化袋子中的数量,以使所有袋子中的数量相同。假设我们采用x个硬币的“ p”个数量和Y个硬币的“ q”个数量,那么任务是使p * X + q * Y最大化。而且,p * X + q * Y <=N。
现在,要找到方程式左手侧的最大可能值,请将p从0更改为N / X,然后找到特定p的最大可能q。然后,在所有此类对中,取(p,q)对,其对为p * X + q * Y的最大值。
下面是上述想法的实现:

C++
// C++ program to find minimum number of bags such
// that each bag contains same amount and sum is at
// least M.
#include
using namespace std;
 
// Return minimum number of bags required.
int minBags(int M, int N, int X, int Y)
{
    // Initialize maximum amount in a bag
    int maxAmount = 0;
 
    // Finding maximum possible q for the particular p.
    for (int p = 0; p <= N/X; p++)
    {
        int q = (N - p * X) / Y;
 
        maxAmount = max(maxAmount, p*X + q*Y);
    }
 
    // Calculating the minimum number of bags.
    int result = M/maxAmount;
    result += (M % maxAmount == 0? 0: 1);
 
    return result;
}
 
// Driven Program
int main()
{
    int M = 45, N = 9;
    int X = 4, Y = 5;
 
    cout << minBags(M, N, X, Y) << endl;
 
    return 0;
}


Java
// Java program to find minimum number
// of bags such that each bag contains
// same amount and sum is at least M
import java.io.*;
 
public class GFG {
     
// Return minimum number of bags required.
static int minBags(int M, int N,
                   int X, int Y)
{
     
    // Initialize maximum amount in a bag
    int maxAmount = 0;
 
    // Finding maximum possible q
    // for the particular p.
    for (int p = 0; p <= N / X; p++)
    {
        int q = (N - p * X) / Y;
 
        maxAmount = Math.max(maxAmount, p * X +
                                        q * Y);
    }
 
    // Calculating the minimum number of bags.
    int result = M / maxAmount;
    result += (M % maxAmount == 0? 0: 1);
 
    return result;
}
 
    // Driver Code
    static public void main (String[] args)
    {
        int M = 45, N = 9;
        int X = 4, Y = 5;
 
        System.out.println(minBags(M, N, X, Y));
    }
}
 
// This code is contributed by vt_m.


Python3
# Python 3 program to find minimum number
# of bags such that each bag contains same
# amount and sum is at least M.
 
# Return minimum number of bags required.
def minBags(M, N, X, Y):
     
    # Initialize maximum amount in a bag
    maxAmount = 0
 
    # Finding maximum possible q for
    # the particular p.
    for p in range(0, int(N / X) + 1, 1):
        q = int((N - p * X) / Y)
 
        maxAmount = max(maxAmount, p * X + q * Y)
 
    # Calculating the minimum number of bags.
    result = int(M / maxAmount)
    if(M % maxAmount == 0):
        result += 0
    else:
        result += 1
 
    return result
 
# Driver Code
if __name__ == '__main__':
    M = 45
    N = 9
    X = 4
    Y = 5
 
    print(minBags(M, N, X, Y))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find minimum number of
// bags such that each bag contains same
// amount and sum is at least M.
using System;
 
public class GFG
{
     
// Return minimum number of bags required.
static int minBags(int M, int N,
                int X, int Y)
{
     
    // Initialize maximum amount in a bag
    int maxAmount = 0;
 
    // Finding maximum possible q
    // for the particular p.
    for (int p = 0; p <= N / X; p++)
    {
        int q = (N - p * X) / Y;
 
        maxAmount = Math.Max(maxAmount, p * X +
                                        q * Y);
    }
 
    // Calculating the minimum number of bags.
    int result = M / maxAmount;
    result += (M % maxAmount == 0? 0: 1);
 
    return result;
}
 
    // Driver Code
    static public void Main ()
    {
        int M = 45, N = 9;
        int X = 4, Y = 5;
 
        Console.WriteLine(minBags(M, N, X, Y));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

5