📜  最大数量可以放在一个篮子里的苹果

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

最大数量可以放在一个篮子里的苹果

给定篮子的“N”个数以及绿色“G”和红色“R”苹果的总数。任务是分配篮子里的所有苹果,并告诉篮子里最多可以放多少个苹果。
注意:没有一个篮子是空的。

例子:

Input: N = 2, R = 1, G = 1
Output:  Maximum apple kept is = 1

Input: N = 2, R = 1, G = 2
Output:  Maximum apple kept is = 2

方法:这个想法是检查no之间的区别。篮子和总数。苹果(红色和绿色) 即首先将 1 个苹果放入 1 个篮子中,这意味着剩余的苹果将是额外的,并且可以放在任何篮子中以使计数最大。因为篮子里已经有 1 个苹果了。因此,苹果的最大数量将是(No_of_apples – No_of_baskets) + 1 。由于提到没有一个篮子是空的,所以苹果总是等于或大于 no。篮子。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function that will calculate the probability
int Number(int Basket, int Red, int Green)
{
    return (Green + Red) - Basket + 1;
}
 
// Driver code
int main()
{
    int Basket = 3, Red = 5, Green = 3;
 
    cout << "Maximum apple kept is = "
         << Number(Basket, Red, Green);
 
    return 0;
}


Java
// Java  implementation of above approach
 
import java.io.*;
 
class GFG {
     
// Function that will calculate the probability
static int Number(int Basket, int Red, int Green)
{
    return (Green + Red) - Basket + 1;
}
 
// Driver code
    public static void main (String[] args) {
         
        int Basket = 3, Red = 5, Green = 3;
 
        System.out.println("Maximum apple kept is = "+
             Number(Basket, Red, Green));
    }
//This Code is Contributed by akt_mit
}


Python3
# Python 3 implementation of above approach
 
# Function that will calculate
# the probability
def Number(Basket, Red, Green):
    return (Green + Red) - Basket + 1
 
# Driver code
if __name__ == '__main__':
    Basket = 3
    Red = 5
    Green = 3
 
    print("Maximum apple kept is =",
           Number(Basket, Red, Green))
     
# This code is contributed by
# Sanjit_Prasad


C#
//C#  implementation of above approach
 
using System;
 
public class GFG{
         
// Function that will calculate the probability
static int Number(int Basket, int Red, int Green)
{
    return (Green + Red) - Basket + 1;
}
 
// Driver code
     
    static public void Main (){
            int Basket = 3, Red = 5, Green = 3;
 
        Console.WriteLine("Maximum apple kept is = "+
            Number(Basket, Red, Green));
    }
//This Code is Contributed by @ajit
}


PHP


Javascript


输出:
Maximum apple kept is = 6