📜  在第N天查找动物园中的小鸡数量

📅  最后修改于: 2021-04-26 19:11:20             🧑  作者: Mango

鉴于动物园只有一只小鸡。一只小鸡每天都会产下2只小鸡,而一只小鸡的预期寿命是6天。任务是日发现在N小鸡的数量。

例子:

简单方法:假设小鸡的预期寿命为6天,因此直到第六天才死亡。当日的每天人口将是前一天的3次。还有一点要注意的是,第4天出生的小鸡不会在该天进行计数,第二天将对其进行计数,并且从第7天开始进行更改。因此,主要计算从第七天开始。

在第七天:从第一天起的小鸡就死了,因此根据手动计算,这将是726。
在第八天:两只新出生的小鸡在第(8-6)天出生,即第二天死亡。这将对当前人口造成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..


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


输出:
9

高效的方法:如果仔细观察,您会发现一种模式,即可以直接使用公式pow(3,N – 1)计算动物园第N天的雏鸡数量。

下面是上述方法的实现:

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
输出:
9