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

📅  最后修改于: 2021-10-27 06:23:05             🧑  作者: 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)