📌  相关文章
📜  求第 N 天动物园里的小鸡数量

📅  最后修改于: 2021-09-17 07:19:04             🧑  作者: Mango

鉴于动物园只有一只小鸡。一只小鸡每天生2只小鸡,一只小鸡的预期寿命是6天。任务是日发现在N小鸡的数量。
例子:

简单的方法:假设一只小鸡的预期寿命是 6 天,所以直到第六天没有小鸡死亡。当天的每日人口将是前一天的3倍。还有一点要注意的是,第i天出生的小鸡当天不计算,第二天计算,从第七天开始变化。所以主要计算从第七天开始。
第七天:从第一天开始的小鸡死亡,因此根据手动计算,它将是 726。
第 8 天:在第 (8-6) 天即第 2 天出生的两只新生小鸡死亡。这将影响当前人口 2/3。这个人口需要从前一天的人口中扣除,因为今天,即第 8 天会有更多的新生儿出生,所以我们不能直接从今天的人口中扣除。由于当天出生的新生儿,这将乘以三倍。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
 
// Function to return the number
// of chicks on the nth day
ll getChicks(int n)
{
 
    // Size of dp[] has to be
    // at least 6 (1-based indexing)
    int size = max(n, 7);
    ll dp[size];
 
    dp[0] = 0;
    dp[1] = 1;
 
    // Every day current population
    // will be three times of the previous day
    for (int i = 2; i <= 6; i++) {
        dp[i] = dp[i - 1] * 3;
    }
 
    // Manually calculated value
    dp[7] = 726;
 
    // From 8th day onwards
    for (int i = 8; i <= n; i++) {
 
        // Chick population decreases by 2/3 everyday.
        // For 8th day on [i-6] i.e 2nd day population
        // was 3 and so 2 new born die on the 6th day
        // and so on for the upcoming days
        dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3;
    }
 
    return dp[n];
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << getChicks(n);
 
    return 0;
}


Java
// Java implementation of the approach
 
import java.util.*;
 
public class GFG {
 
 
// Function to return the number
// of chicks on the nth day
static long getChicks(int n)
{
 
    // Size of dp[] has to be
    // at least 6 (1-based indexing)
    int size = Math.max(n, 7);
    long []dp = new long[size];
 
    dp[0] = 0;
    dp[1] = 1;
 
    // Every day current population
    // will be three times of the previous day
    for (int i = 2; i < 6; i++) {
        dp[i] = dp[i - 1] * 3;
    }
 
    // Manually calculated value
    dp[6] = 726;
 
    // From 8th day onwards
    for (int i = 8; i <= n; i++) {
 
        // Chick population decreases by 2/3 everyday.
        // For 8th day on [i-6] i.e 2nd day population
        // was 3 and so 2 new born die on the 6th day
        // and so on for the upcoming days
        dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3;
    }
 
    return dp[n];
}
 
// Driver code
public static void main(String[] args) {
int n = 3;
 
    System.out.println(getChicks(n));
    }
}
// This code has been contributed by 29AjayKumar


Python3
# Python implementation of the approach
  
# Function to return the number
# of chicks on the nth day
def getChicks(n):
  
    # Size of dp[] has to be
    # at least 6 (1-based indexing)
    size = max(n, 7);
    dp = [0]*size;
  
    dp[0] = 0;
    dp[1] = 1;
  
    # Every day current population
    # will be three times of the previous day
    for i in range(2,7):
        dp[i] = dp[i - 1] * 3;
  
    # Manually calculated value
    dp[6] = 726;
  
    # From 8th day onwards
    for i in range(8,n+1):
  
        # Chick population decreases by 2/3 everyday.
        # For 8th day on [i-6] i.e 2nd day population
        # was 3 and so 2 new born die on the 6th day
        # and so on for the upcoming days
        dp[i] = (dp[i - 1] - (2 * dp[i - 6] // 3)) * 3;
  
    return dp[n];
  
# Driver code
n = 3;
  
print(getChicks(n));
 
# This code is contributed by Princi Singh


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the number
// of chicks on the nth day
static long getChicks(int n)
{
 
    // Size of dp[] has to be
    // at least 6 (1-based indexing)
    int size = Math.Max(n, 7);
    long []dp = new long[size];
 
    dp[0] = 0;
    dp[1] = 1;
 
    // Every day current population
    // will be three times of the previous day
    for (int i = 2; i < 6; i++)
    {
        dp[i] = dp[i - 1] * 3;
    }
 
    // Manually calculated value
    dp[6] = 726;
 
    // From 8th day onwards
    for (int i = 8; i <= n; i++)
    {
 
        // Chick population decreases by 2/3 everyday.
        // For 8th day on [i-6] i.e 2nd day population
        // was 3 and so 2 new born die on the 6th day
        // and so on for the upcoming days
        dp[i] = (dp[i - 1] - (2 * dp[i - 6] / 3)) * 3;
    }
 
    return dp[n];
}
 
// Driver code
static public void Main ()
{
     
    int n = 3;
    Console.WriteLine(getChicks(n));
}
}
 
// This code has been contributed by @Tushil..


Javascript


C++
// C++ implementation of the approach
 
#include 
using namespace std;
#define ll long long int
 
// Function to return the number
// of chicks on the nth day
ll getChicks(int n)
{
 
    ll chicks = (ll)pow(3, n - 1);
 
    return chicks;
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << getChicks(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the number
// of chicks on the nth day
static int getChicks(int n)
{
 
    int chicks = (int)Math.pow(3, n - 1);
 
    return chicks;
}
 
// Driver code
public static void main (String[] args)
{
 
    int n = 3;
    System.out.println (getChicks(n));
}
}
 
// This code is contributed by Tushil.


Python 3
# Python 3 implementation of the approach
 
# Function to return the number
# of chicks on the nth day
def getChicks( n):
 
    chicks = pow(3, n - 1)
 
    return chicks
 
# Driver code
if __name__ == "__main__":
    n = 3
 
    print ( getChicks(n))
 
# This code is contributed by ChitraNayal


C#
// C# implementation of the approach
using System;
 
class GFG
{
         
    // Function to return the number
    // of chicks on the nth day
    static int getChicks(int n)
    {
     
        int chicks = (int)Math.Pow(3, n - 1);
     
        return chicks;
    }
     
    // Driver code
    public static void Main()
    {
     
        int n = 3;
        Console.WriteLine(getChicks(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:

9

有效的方法:如果仔细观察,您可以观察到一个模式,即动物园第 N 天的小鸡数量可以直接使用公式pow(3, N – 1) 计算
下面是上述方法的实现:

C++

// C++ implementation of the approach
 
#include 
using namespace std;
#define ll long long int
 
// Function to return the number
// of chicks on the nth day
ll getChicks(int n)
{
 
    ll chicks = (ll)pow(3, n - 1);
 
    return chicks;
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << getChicks(n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the number
// of chicks on the nth day
static int getChicks(int n)
{
 
    int chicks = (int)Math.pow(3, n - 1);
 
    return chicks;
}
 
// Driver code
public static void main (String[] args)
{
 
    int n = 3;
    System.out.println (getChicks(n));
}
}
 
// This code is contributed by Tushil.

Python3

# Python 3 implementation of the approach
 
# Function to return the number
# of chicks on the nth day
def getChicks( n):
 
    chicks = pow(3, n - 1)
 
    return chicks
 
# Driver code
if __name__ == "__main__":
    n = 3
 
    print ( getChicks(n))
 
# This code is contributed by ChitraNayal

C#

// C# implementation of the approach
using System;
 
class GFG
{
         
    // Function to return the number
    // of chicks on the nth day
    static int getChicks(int n)
    {
     
        int chicks = (int)Math.Pow(3, n - 1);
     
        return chicks;
    }
     
    // Driver code
    public static void Main()
    {
     
        int n = 3;
        Console.WriteLine(getChicks(n));
    }
}
 
// This code is contributed by AnkitRai01

Javascript


输出:
9

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程