📜  从树上收集的最少苹果数量以保证 M 个红苹果

📅  最后修改于: 2021-10-26 06:37:50             🧑  作者: Mango

四个方向(东、西、北、南)有不同种类的苹果树,它们可能会同时长出红苹果和绿苹果,这样每棵树都可以长出 K 个苹果,具体方式如下:

  • N——北边没有红苹果的树的数量。
  • S – 南方没有青苹果的树的数量。
  • W – 西部有一些红苹果的树的数量。
  • E – 东部的树木数量有一些青苹果。

然而,在屋外无法分辨苹果的颜色。因此,任务是找到要从树中收集的最少苹果数量,以保证有 M 个红苹果。如果不可能,则打印 -1。

例子:

做法:南方的每个苹果都保证它是红色的。所以首先,从南方拿一个苹果。在东方和西方,每棵树上至少有 1 个红苹果。这就是为什么,为了保证它被认为东、西每棵树上只有1个红苹果。北方没有红苹果,所以,忽略这一点。请按照以下步骤解决问题:

  • 如果M小于等于S*K,则打印M。
  • 否则,如果M小于等于S*K+E+W,则打印S*K + (MS*K) * K
  • 否则打印-1。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to minimum no. of apples
int minApples(int M,int K,int N,int S,int W,int E){
    // If we get all required apple
    // from South
    if(M <= S * K)
        return M;
 
    // If we required trees at
    // East and West
    else if(M <= S * K + E + W)
        return S * K + (M-S * K) * K;
 
    // If we doesn't have enough
    // red apples
    else
        return -1;
 
}
 
// Driver Code
int main(){
     
    // No. of red apple for gift
    int M = 10;
 
    // No. of red apple in each tree
    int K = 15;
 
    // No. of tree in North
    int N = 0;
 
    // No. of tree in South
    int S = 1;
 
    // No. of tree in West
    int W = 0;
 
    // No. of tree in East
    int E = 0;
 
    // Function Call
    int ans = minApples(M,K,N,S,W,E);
    cout<


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
// Function to minimum no. of apples
static int minApples(int M,int K,int N,int S,int W,int E)
{
   
    // If we get all required apple
    // from South
    if(M <= S * K)
        return M;
 
    // If we required trees at
    // East and West
    else if(M <= S * K + E + W)
        return S * K + (M-S * K) * K;
 
    // If we doesn't have enough
    // red apples
    else
        return -1;
}
 
// Driver code
public static void main(String[] args)
{
    // No. of red apple for gift
    int M = 10;
 
    // No. of red apple in each tree
    int K = 15;
 
    // No. of tree in North
    int N = 0;
 
    // No. of tree in South
    int S = 1;
 
    // No. of tree in West
    int W = 0;
 
    // No. of tree in East
    int E = 0;
 
    // Function Call
    int ans = minApples(M,K,N,S,W,E);
    System.out.println(ans);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
 
 
# Function to minimum no. of apples
def minApples():
 
    # If we get all required apple
    # from South
    if M <= S * K:
        return M
 
    # If we required trees at
    # East and West
    elif M <= S * K + E + W:
        return S * K + (M-S * K) * K
 
    # If we doesn't have enough
    # red apples
    else:
        return -1
 
 
# Driver Code
if __name__ == "__main__":
 
    # No. of red apple for gift
    M = 10
 
    # No. of red apple in each tree
    K = 15
 
    # No. of tree in North
    N = 0
 
    # No. of tree in South
    S = 1
 
    # No. of tree in West
    W = 0
 
    # No. of tree in East
    E = 0
 
    # Function Call
    ans = minApples()
    print(ans)


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to minimum no. of apples
static int minApples(int M, int K, int N,
                     int S, int W, int E)
{
     
    // If we get all required apple
    // from South
    if (M <= S * K)
        return M;
 
    // If we required trees at
    // East and West
    else if (M <= S * K + E + W)
        return S * K + (M - S * K) * K;
 
    // If we doesn't have enough
    // red apples
    else
        return -1;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // No. of red apple for gift
    int M = 10;
 
    // No. of red apple in each tree
    int K = 15;
 
    // No. of tree in North
    int N = 0;
 
    // No. of tree in South
    int S = 1;
 
    // No. of tree in West
    int W = 0;
 
    // No. of tree in East
    int E = 0;
 
    // Function Call
    int ans = minApples(M, K, N, S, W, E);
    Console.Write(ans);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
10

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