📌  相关文章
📜  检查是否可以将所有类型A和B的物品放在N个架子上

📅  最后修改于: 2021-06-25 11:10:45             🧑  作者: Mango

给定两个整数AB ,分别代表两种不同类型的对象的数量,另一个整数N代表架子的数量,任务是按照以下规则将所有对象放置在给定的N个架子中:

  • 任何架子都不能同时包含Type-A和Type-B对象。
  • 架子最多只能容纳K个A型对象或L个B型对象。

如果可以将所有物品放置在N个货架上,则打印“是” 。否则,打印“否”
例子:

方法:
为了解决该问题,我们需要计算放置所有物体所需的最小货架数,并检查其是否超过N。请按照以下步骤操作:

  • 计算放置Type-A项目所需的最小项目数,例如needa 。由于K个A型物品最多只能放在一个架子上,因此会出现以下两种情况:
    1. 如果A可被K整除,则所有Type-A物品都可以放在A / K货架上。
    2. 否则,必须将A%K的物品放在一个架子上,其余的放在A / K架子上,因此这种情况下需要A / K + 1个架子。
  • 同样,计算放置B型物品所需的最少货架数,例如Needb
  • 如果Needa + Needb超过N ,则无法分配。否则,这是可能的。

下面是上述方法的实现。

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to return if allocation
// is possible or not
bool isPossible(int A, int B, int N,
                int K, int L)
{
    // Stores the shelves needed
    // for items of type-A and type-B
    int needa, needb;
 
    // Find number of shelves
    // needed for items of type-A
    if (A % K == 0)
 
        // Fill A / K shelves fully
        // by the items of type-A
        needa = A / K;
 
    // Otherwise
    else
 
        // Fill A / L shelves fully
        // and add remaining to an
        // extra shelf
        needa = A / K + 1;
 
    // Find number of shelves
    // needed for items of type-B
    if (B % L == 0)
 
        // Fill B / L shelves fully
        // by the items of type-B
        needb = B / L;
 
    else
 
        // Fill B / L shelves fully
        // and add remaining to an
        // an extra shelf
        needb = B / L + 1;
 
    // Total shelves needed
    int total = needa + needb;
 
    // If required shelves exceed N
    if (total > N)
        return false;
    else
        return true;
}
 
// Driver Program
int main()
{
    int A = 3, B = 3, N = 3;
    int K = 4, M = 2;
 
    if (isPossible(A, B, N, K, M))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to return if allocation
// is possible or not
static boolean isPossible(int A, int B,
                          int N, int K,
                          int L)
{
     
    // Stores the shelves needed
    // for items of type-A and type-B
    int needa, needb;
 
    // Find number of shelves
    // needed for items of type-A
    if (A % K == 0)
 
        // Fill A / K shelves fully
        // by the items of type-A
        needa = A / K;
 
    // Otherwise
    else
 
        // Fill A / L shelves fully
        // and add remaining to an
        // extra shelf
        needa = A / K + 1;
 
    // Find number of shelves
    // needed for items of type-B
    if (B % L == 0)
 
        // Fill B / L shelves fully
        // by the items of type-B
        needb = B / L;
 
    else
 
        // Fill B / L shelves fully
        // and add remaining to an
        // an extra shelf
        needb = B / L + 1;
 
    // Total shelves needed
    int total = needa + needb;
 
    // If required shelves exceed N
    if (total > N)
        return false;
    else
        return true;
}
 
// Driver code
public static void main(String[] args)
{
    int A = 3, B = 3, N = 3;
    int K = 4, M = 2;
 
    if (isPossible(A, B, N, K, M))
        System.out.print("YES" + "\n");
    else
        System.out.print("NO" + "\n");
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation of the
# above approach
 
# Function to return if allocation
# is possible or not
def isPossible(A, B, N, K, L):
     
    # Stores the shelves needed
    # for items of type-A and type-B
    needa = 0
    needb = 0
 
    # Find number of shelves
    # needed for items of type-A
    if (A % K == 0):
 
        # Fill A / K shelves fully
        # by the items of type-A
        needa = A // K;
 
    # Otherwise
    else:
 
        # Fill A / L shelves fully
        # and add remaining to an
        # extra shelf
        needa = A // K + 1
 
    # Find number of shelves
    # needed for items of type-B
    if (B % L == 0):
 
        # Fill B / L shelves fully
        # by the items of type-B
        needb = B // L
 
    else:
 
        # Fill B / L shelves fully
        # and add remaining to an
        # an extra shelf
        needb = B // L + 1
 
    # Total shelves needed
    total = needa + needb
 
    # If required shelves exceed N
    if (total > N):
        return False
    else:
        return True
 
# Driver Code       
if __name__=='__main__':
     
    A, B, N = 3, 3, 3
    K, M = 4, 2
 
    if (isPossible(A, B, N, K, M)):
        print('YES')
    else:
        print('NO')
 
# This code is contributed by rutvik_56


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
// Function to return if allocation
// is possible or not
static bool isPossible(int A, int B,
                       int N, int K,
                       int L)
{
     
    // Stores the shelves needed
    // for items of type-A and type-B
    int needa, needb;
 
    // Find number of shelves
    // needed for items of type-A
    if (A % K == 0)
 
        // Fill A / K shelves fully
        // by the items of type-A
        needa = A / K;
 
    // Otherwise
    else
 
        // Fill A / L shelves fully
        // and add remaining to an
        // extra shelf
        needa = A / K + 1;
 
    // Find number of shelves
    // needed for items of type-B
    if (B % L == 0)
 
        // Fill B / L shelves fully
        // by the items of type-B
        needb = B / L;
 
    else
 
        // Fill B / L shelves fully
        // and add remaining to an
        // an extra shelf
        needb = B / L + 1;
 
    // Total shelves needed
    int total = needa + needb;
 
    // If required shelves exceed N
    if (total > N)
        return false;
    else
        return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int A = 3, B = 3, N = 3;
    int K = 4, M = 2;
 
    if (isPossible(A, B, N, K, M))
        Console.Write("YES" + "\n");
    else
        Console.Write("NO" + "\n");
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
YES

时间复杂度: O(1)
辅助空间: O(1)