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

你是岛上的穷人。这个岛上只有一家商店,除了星期天,这家商店一周的所有日子都营业。考虑以下约束:

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

现在是星期一,你需要在接下来的几天里活下来。 找出你需要在商店购买食物的最短天数,这样你才能在接下来的几天里生存下来, 或者确定它不可能生存。 例如:

输入:S=10 N=16 M=2 输出:是2 解释1: 一个可能的解决方案是在第一天(星期一)买一盒,从这个盒子吃到第八天(星期一)就足够了。现在,在第9天(星期二),你买了另一盒巧克力,用里面的巧克力度过第9天和第10天。 输入:102030 输出:否 解释2:即使你买了食物,你也活不下去,因为你一天能买的最大单位数少于一天所需的食物。

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

注意:我们在前6天购买食物,因为我们从周一开始计算,周日商店将继续营业。 如果上述任何一种情况都不成立,那么我们就无法生存下去

该方程可推导为:

我们应该购买的食物量>=生存所需的食物量。–>方程式1

我们购买的食物数量=我们购买的次数(天)*我们一次购买的食物数量(N)

生存所需的食物量=我们生存所需的天数(S)*每天所需的食物量(M)。

现在从我们的方程式1:

天*N>=S*M

因此,天数=ceil(S*M)/N

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 <bits/stdc++.h>
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" ;
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

<?php
// PHP 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
// Function to find
// the minimum $days
function 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) &&
$S > 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

<script>
// JavaScript 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
// function to find the minimum days
function 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) && S > 6) || M > N)
document.write( "No" );
else {
// If we can survive then we can
// buy ceil(A/N) times where A is
// total units of food required.
let days = (M * S) / N;
if (((M * S) % N) != 0)
days++;
document.write( "Yes " + Math.round(days));
}
}
// Driver Code
let S = 10, N = 16, M = 2;
survival(S, N, M);
// This code is contributed by splevel62.
</script>


输出:

Yes 2

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

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享