📜  检查岛上是否有可能生存

📅  最后修改于: 2021-04-29 18:43:47             🧑  作者: Mango

你是一个岛上的穷人。这个岛上只有一家商店,这家商店在星期天(周日除外)全天营业。考虑以下约束:

  • N –您每天可以购买的最大食物单位。
  • S –您需要生存的天数。
  • M –每天生存所需的食物单位。

目前是星期一,您需要在接下来的S天中生存。
找到您需要从商店购买食物的最短天数,以便您可以在接下来的S天生存,或者确定无法生存。
例子:

方法:
在这个问题上,贪婪的方法是连续几天购买食物是正确的方向。
如果我们可以在前7天生存下来,那么我们可以在任何天数生存下来,因此我们需要检查两件事
->检查我们是否可以生存一天。
->(S> = 7)如果我们在一周的前6天购买食物并且我们可以生存一周,即一周内可以购买的总食物量(6 * N)大于或等于我们的总食物量需要生存一周(7 * M),我们才能生存。

注意:我们会在前6天购买食物,因为我们从星期一开始计数,而商店将在周日关闭。
如果以上任何条件都不成立,那么我们将无法生存,否则购买食物所需的最少天数将为ceil(所需总食物量/我们每天可以购买的食物单位)。

CPP
// C++ program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
#include 
using namespace std;
 
// function to find the minimum days
void survival(int S, int N, int M)
{
 
    // If we can not buy at least a week
    // supply of food during the first week
    // OR We can not buy a day supply of food
    // on the first day then we can't survive.
    if (((N * 6) < (M * 7) && S > 6) || M > N)
        cout << "No\n";
    else {
        // If we can survive then we can
        // buy ceil(A/N) times where A is
        // total units of food required.
        int days = (M * S) / N;
        if (((M * S) % N) != 0)
            days++;
        cout << "Yes " << days << endl;
    }
}
 
// Driver code
int main()
{
    int S = 10, N = 16, M = 2;
    survival(S, N, M);
    return 0;
}


Java
// Java program to find the minimum days on which
// you need to buy food from the shop so that you
// can survive the next S days
import java.io.*;
 
class GFG {
 
    // function to find the minimum days
    static void survival(int S, int N, int M)
    {
 
        // If we can not buy at least a week
        // supply of food during the first
        // week OR We can not buy a day supply
        // of food on the first day then we
        // can't survive.
        if (((N * 6) < (M * 7) && S > 6) || M > N)
            System.out.println("No");
 
        else {
 
            // If we can survive then we can
            // buy ceil(A/N) times where A is
            // total units of food required.
            int days = (M * S) / N;
 
            if (((M * S) % N) != 0)
                days++;
 
            System.out.println("Yes " + days);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int S = 10, N = 16, M = 2;
 
        survival(S, N, M);
    }
}
 
// This code is contributed by vt_m.


Python3
# Python3 program to find the minimum days on 
# which you need to buy food from the shop so
# that you can survive the next S days
def survival(S, N, M):
 
# If we can not buy at least a week
# supply of food during the first week
# OR We can not buy a day supply of food
# on the first day then we can't survive.
    if (((N * 6) < (M * 7) and S > 6) or M > N):
        print("No")
    else:
         
    # If we can survive then we can
    # buy ceil(A / N) times where A is
    # total units of food required.
        days = (M * S) / N
         
        if (((M * S) % N) != 0):
            days += 1
        print("Yes "),
        print(days)
 
# Driver code
S = 10; N = 16; M = 2
survival(S, N, M)
 
# This code is contributed by upendra bartwal


C#
// C# program to find the minimum days
// on which you need to buy food from
// the shop so that you can survive
// the next S days
using System;
 
class GFG {
 
    // function to find the minimum days
    static void survival(int S, int N, int M)
    {
 
        // If we can not buy at least a week
        // supply of food during the first
        // week OR We can not buy a day
        // supply of food on the first day
        // then we can't survive.
        if (((N * 6) < (M * 7) && S > 6) || M > N)
            Console.Write("No");
        else {
             
            // If we can survive then we can
            // buy ceil(A/N) times where A is
            // total units of food required.
            int days = (M * S) / N;
             
            if (((M * S) % N) != 0)
                days++;
                 
            Console.WriteLine("Yes " + days);
        }
    }
 
    // Driver code
    public static void Main()
    {
        int S = 10, N = 16, M = 2;
         
        survival(S, N, M);
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


PHP
 6) || $M >$N)
        echo "No";
    else
    {
         
        // If we can survive then we can
        // buy ceil(A/N) times where A is
        // total units of food required.
        $days = ($M * $S) / $N;
        if ((($M * $S) % $N) != 0)
            $days++;
        echo "Yes " , floor($days) ;
    }
}
 
    // Driver code
    $S = 10; $N = 16; $M = 2;
    survival($S, $N, $M);
     
// This code is contributed by anuj_67
 
?>


Javascript


输出:

Yes 2

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