📜  极客数

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

给定四个整数ABCN ,其中ABC代表geekonacci系列的前三个数字,任务是找到Geekonacci系列的N

例子:

方法:给定的问题可以通过产生的geekonacci系列高达N项来解决,并打印所述N个所获得的系列的术语。请按照以下步骤解决此问题:

  • 初始化大小为N的数组arr [] ,并初始化arr [0] = Aarr [1] = Barr [2] = C。
  • 迭代范围[3,N – 1]并更新每个i元素的值,即arr [i](arr [i – 1] + arr [i – 2] + arr [i – 3])来获得geekonacci系列的i学期
  • 完成上述步骤后,将arr [N – 1]的值打印为geekonacci系列的N数字

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to calculate the
// N-th Geek-onacci Number
int find(int A, int B,
                int C, int N)
{
   
    // Stores the geekonacci series
    int arr[N];
 
    // Store the first three
    // terms of the series
    arr[0] = A;
    arr[1] = B;
    arr[2] = C;
 
    // Iterate over the range [3, N]
    for (int i = 3; i < N; i++) {
 
        // Update the value of arr[i]
        // as the sum of previous 3
        // terms in the series
        arr[i] = arr[i - 1]
                 + arr[i - 2]
                 + arr[i - 3];
    }
 
    // Return the last element
    // of arr[] as the N-th term
    return arr[N - 1];
}
 
// Driver Code
int main()
{
  int A = 1, B = 3, C = 2, N = 4;
  cout<<(find(A, B, C, N));
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to calculate the
    // N-th Geek-onacci Number
    static int find(int A, int B,
                    int C, int N)
    {
        // Stores the geekonacci series
        int[] arr = new int[N];
 
        // Store the first three
        // terms of the series
        arr[0] = A;
        arr[1] = B;
        arr[2] = C;
 
        // Iterate over the range [3, N]
        for (int i = 3; i < N; i++) {
 
            // Update the value of arr[i]
            // as the sum of previous 3
            // terms in the series
            arr[i] = arr[i - 1]
                     + arr[i - 2]
                     + arr[i - 3];
        }
 
        // Return the last element
        // of arr[] as the N-th term
        return arr[N - 1];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 1, B = 3, C = 2, N = 4;
        System.out.print(find(A, B, C, N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to calculate the
# N-th Geek-onacci Number
def find(A, B, C, N) :
   
    # Stores the geekonacci series
    arr = [0] * N
 
    # Store the first three
    # terms of the series
    arr[0] = A
    arr[1] = B
    arr[2] = C
 
    # Iterate over the range [3, N]
    for i in range(3, N):
 
        # Update the value of arr[i]
        # as the sum of previous 3
        # terms in the series
        arr[i] = (arr[i - 1]
                 + arr[i - 2]
                 + arr[i - 3])
     
    # Return the last element
    # of arr[] as the N-th term
    return arr[N - 1]
 
# Driver Code
A = 1
B = 3
C = 2
N = 4
 
print(find(A, B, C, N))
 
# This code is contributed by sanjoy_62.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to calculate the
  // N-th Geek-onacci Number
  static int find(int A, int B,
                  int C, int N)
  {
    // Stores the geekonacci series
    int[] arr = new int[N];
 
    // Store the first three
    // terms of the series
    arr[0] = A;
    arr[1] = B;
    arr[2] = C;
 
    // Iterate over the range [3, N]
    for (int i = 3; i < N; i++) {
 
      // Update the value of arr[i]
      // as the sum of previous 3
      // terms in the series
      arr[i] = arr[i - 1]
        + arr[i - 2]
        + arr[i - 3];
    }
 
    // Return the last element
    // of arr[] as the N-th term
    return arr[N - 1];
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int A = 1, B = 3, C = 2, N = 4;
    Console.Write(find(A, B, C, N));
  }
}
 
// This code is contributed by code_hunt.


输出:
6

时间复杂度: O(N)
辅助空间: O(N)