📜  分发 R,B bean,使得每个数据包至少有 1 个 R 和 1 个 B bean,绝对差异最多 D

📅  最后修改于: 2022-05-13 01:56:08.197000             🧑  作者: Mango

分发 R,B bean,使得每个数据包至少有 1 个 R 和 1 个 B bean,绝对差异最多 D

给定两个正整数RB代表R红色和B蓝色 bean 和一个整数D ,任务是检查是否可以根据以下规则将 bean 分配到几个(可能是一个)数据包中:

  • 每包至少有一颗红豆。
  • 每包至少有一颗蓝豆。
  • 每个数据包中红豆和蓝豆的数量应相差不超过D(|RB| <= D)

如果可能,请打印Yes 。否则,打印No

例子

方法:通过观察(RB)的最大值是RB的最小值的D + 1倍,可以轻松解决这个问题。请按照以下步骤解决问题:

  • R 和 B的最大值。 以及R 和 B的最小值。
  • 为了满足给定的 3 个约束, max(R, B)的值最多应为(D + 1)乘以min(R, B) ,因为每个数据包中可以保留(D + 1)个 bean,1 个 bean一种类型,另一种类型的D豆。
  • 完成上述步骤后,如果max(R, B)的值小于或等于(D + 1)*min(R, B) ,则打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to distribute R red and B blue beans
// in packets such that the difference
// between the beans in each packet is
// atmost D
void checkDistribution(int R, int B, int D)
{
    // Check for the condition to
    // distributing beans
    if (max(R, B) <= min(R, B) * (D + 1)) {
 
        // Print the answer
        cout << "Yes";
    }
 
    // Distribution is not possible
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int R = 1, B = 1, D = 0;
    checkDistribution(R, B, D);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to check if it is possible
    // to distribute R red and B blue beans
    // in packets such that the difference
    // between the beans in each packet is
    // atmost D
    static void checkDistribution(int R, int B, int D)
    {
       
        // Check for the condition to
        // distributing beans
        if (Math.max(R, B) <= Math.min(R, B) * (D + 1)) {
 
            // Print the answer
            System.out.println("Yes");
        }
 
        // Distribution is not possible
        else {
            System.out.println("No");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int R = 1, B = 1, D = 0;
        checkDistribution(R, B, D);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to check if it is possible
# to distribute R red and B blue beans
# in packets such that the difference
# between the beans in each packet is
# atmost D
def checkDistribution(R, B, D):
     
    # Check for the condition to
    # distributing beans
    if (max(R, B) <= min(R, B) * (D + 1)):
         
        # Print the answer
        print("Yes")
     
    # Distribution is not possible
    else:
        print("No")
 
# Driver Code
R = 1
B = 1
D = 0
 
checkDistribution(R, B, D)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
 
class GFG{
     
    // Function to check if it is possible
    // to distribute R red and B blue beans
    // in packets such that the difference
    // between the beans in each packet is
    // atmost D
    static void checkDistribution(int R, int B, int D)
    {
       
        // Check for the condition to
        // distributing beans
        if (Math.Max(R, B) <= Math.Min(R, B) * (D + 1)) {
 
            // Print the answer
            Console.WriteLine("Yes");
        }
 
        // Distribution is not possible
        else {
            Console.WriteLine("No");
        }
    }
 
 
// Driver code
static public void Main()
{
    int R = 1, B = 1, D = 0;
    checkDistribution(R, B, D);
}
}
 
// This code is contributed by target_2.


Javascript


输出
Yes

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