📌  相关文章
📜  Geek-onacci 数

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

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

例子:

方法:可以通过生成最多N 项的geekonacci 级数并打印所获得级数的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.


Javascript


输出:
6

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