给定四个整数A 、 B 、 C和N ,其中A 、 B 、 C代表geekonacci 级数的前三个数字,任务是找到geekonacci 级数的第N 项。
The Nth term of the geekonacci series is the sum of its previous three terms in the series i.e., sum of (N – 1)th, (N – 2)th, and (N – 3)th geekonacci numbers.
例子:
Input: A = 1, B = 3, C = 2, N = 4
Output: 6
Explanation: The geekonacci series is 1, 3, 2, 6, 11, 19, ……
Therefore, the 4th geekonacci number is 6.
Input: A = 1, B = 3, C = 2, N = 6
Output: 19
方法:可以通过生成最多N 项的geekonacci 级数并打印所获得级数的第N 项来解决给定的问题。请按照以下步骤解决此问题:
- 初始化大小为N的数组arr[]并初始化arr[0] = A 、 arr[1] = B和arr[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)