📜  在 N 个男孩中分配 C 个糖果,使得收到的最大和最小糖果之间的差异为 K

📅  最后修改于: 2021-10-26 05:10:13             🧑  作者: Mango

给定两个整数NC ,代表男孩和糖果的数量,以及一个整数K ,任务是计算任何男孩收到的最大和最小糖果数量,使它们之间的差为K

例子:

方法:根据以下观察可以解决给定的问题:

  1. 如果 K >= C :所有C糖果都可以分配给第一个男孩。因此,糖果的最大数量为C ,最小数量为0
  2. 否则,将糖果分发给保持最大和最小计数≤ K 的男孩

请按照以下步骤解决问题:

  1. 初始化两个变量,比如最大值最小值,以存储一个男孩可以拥有的最大和最小糖果数量。
  2. 如果 N = 1:设置最大值 = C最小值 = C
  3. 如果 K >= C:设置最大值= C最小值= 0
  4. 否则,如果K < C ,则设置最大值 = K最小值 = 0 。现在,请按照以下步骤操作:
    1. 初始化一个变量,比如continue_candy,并将其设置为C – K。
    2. 将 ( remain_candy/N ) 添加到最大值最小值
    3. 如果( remain_candy % N == N-1 ),增加最小值
  5. 打印最小值最大值

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
  
// Function to calculate the
// maximum and minimum number
// of candies a boy can possess
int max_min(int N, int C, int K)
{
    int maximum, minimum;
  
    if (N == 1) {
  
        // All candies will be
        // given to one boy
        maximum = minimum = C;
    }
  
    else if (K >= C) {
  
        // All the candies will
        // be given to 1 boy
        maximum = C;
        minimum = 0;
    }
  
    else {
  
        // Give K candies to 1st
        // boy initially
        maximum = K;
        minimum = 0;
  
        // Count remaining candies
        int remain_candy = C - K;
  
        maximum += remain_candy / N;
        minimum = remain_candy / N;
  
        // If the last candy of remaining candies
        // is given to the last boy, i.e Nth boy
        if (remain_candy % N == N - 1) {
  
            // Increase minimum count
            minimum++;
        }
    }
  
    cout << "Maximum = " << maximum << endl;
    cout << "Minimum = " << minimum;
  
    return 0;
}
  
// Driver Code
int main()
{
    int N = 4;
    int C = 12;
    int K = 3;
  
    max_min(N, C, K);
  
    return 0;
}


Java
// Java program for the above approach
class GFG{
      
// Function to calculate the
// maximum and minimum number
// of candies a boy can possess
static void max_min(int N, int C, int K)
{
    int maximum, minimum;
  
    if (N == 1)
    {
          
        // All candies will be
        // given to one boy
        maximum = minimum = C;
    }
  
    else if (K >= C) 
    {
          
        // All the candies will
        // be given to 1 boy
        maximum = C;
        minimum = 0;
    }
  
    else 
    {
          
        // Give K candies to 1st
        // boy initially
        maximum = K;
        minimum = 0;
  
        // Count remaining candies
        int remain_candy = C - K;
  
        maximum += remain_candy / N;
        minimum = remain_candy / N;
  
        // If the last candy of remaining candies
        // is given to the last boy, i.e Nth boy
        if (remain_candy % N == N - 1) 
        {
              
            // Increase minimum count
            minimum++;
        }
    }
    System.out.println("Maximum = " + maximum);
    System.out.println("Minimum = " + minimum);
}
  
// Driver code
public static void main(String[] args)
{
    int N = 4;
    int C = 12;
    int K = 3;
  
    max_min(N, C, K);
}
}
  
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
  
# Function to calculate the
# maximum and minimum number
# of candies a boy can possess
def max_min(N, C, K):
      
    maximum = 0
    minimum = 0
      
    if (N == 1):
          
        # All candies will be
        # given to one boy
        maximum = minimum = C
  
    elif (K >= C):
          
        # All the candies will
        # be given to 1 boy
        maximum = C
        minimum = 0
  
    else:
          
        # Give K candies to 1st
        # boy initially
        maximum = K
        minimum = 0
  
        # Count remaining candies
        remain_candy = C - K
  
        maximum += remain_candy // N
        minimum = remain_candy // N
  
    # If the last candy of remaining candies
    # is given to the last boy, i.e Nth boy
    if (remain_candy % N == N - 1):
          
        # Increase minimum count
        minimum += 1
  
    print("Maximum = {}".format(maximum))
    print("Minimum = {}".format(minimum))
  
# Driver code
N = 4
C = 12
K = 3
  
max_min(N, C, K)
  
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to calculate the
// maximum and minimum number
// of candies a boy can possess
static void max_min(int N, int C, int K)
{
    int maximum, minimum;
  
    if (N == 1) 
    {
          
        // All candies will be
        // given to one boy
        maximum = minimum = C;
    }
  
    else if (K >= C) 
    {
          
        // All the candies will
        // be given to 1 boy
        maximum = C;
        minimum = 0;
    }
  
    else 
    {
          
        // Give K candies to 1st
        // boy initially
        maximum = K;
        minimum = 0;
  
        // Count remaining candies
        int remain_candy = C - K;
  
        maximum += remain_candy / N;
        minimum = remain_candy / N;
  
        // If the last candy of remaining candies
        // is given to the last boy, i.e Nth boy
        if (remain_candy % N == N - 1)
        {
              
            // Increase minimum count
            minimum++;
        }
    }
    Console.WriteLine("Maximum = " + maximum);
    Console.WriteLine("Minimum = " + minimum);
}
  
// Driver code
static void Main()
{
    int N = 4;
    int C = 12;
    int K = 3;
  
    max_min(N, C, K);
}
}
  
// This code is contibuted by abhinavjain194


Javascript


输出:
Maximum = 5
Minimum = 2

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