给定一个整数 n。我们必须找到斯特恩双原子级数的第 n 项。
Stern 的双原子级数是生成以下整数序列 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, ……的序列。它出现在 Calkin-Wilf 树中。它有时也称为fusc函数。
在数学方面,Stern 双原子级数的序列 P(n) 由递推关系定义。
例子 :
Input : n = 7
Output : 3
Input : n = 15
Output : 4
方法 :
我们用一个非常简单的动态规划概念来解决这个问题,该概念用于寻找斐波那契数。在保存 DP[0] = 0, DP[1] = 1 的基本情况后,我们必须简单地从 i = 2 遍历到 n 并按照 Stern 双原子级数的解释定义计算 DP[i]。最后返回DP[n]的值。
算法 :
// SET the Base case
DP[0] = 0;
DP[1] = 1;
// Traversing the array from 2nd Element to nth Element
for (int i=2; i<=n; i++)
{
// Case 1: for even n
if (i%2 == 0)
DP[i] = DP[i/2];
// Case 2: for odd n
else
DP[i] = DP[(i-1)/2] + DP[(i+1)/2];
}
return DP[n];
C++
// Program to find the nth element
// of Stern's Diatomic Series
#include
using namespace std;
// function to find nth stern'
// diatomic series
int findSDSFunc(int n)
{
// Initializing the DP array
int DP[n+1];
// SET the Base case
DP[0] = 0;
DP[1] = 1;
// Traversing the array from
// 2nd Element to nth Element
for (int i = 2; i <= n; i++) {
// Case 1: for even n
if (i % 2 == 0)
DP[i] = DP[i / 2];
// Case 2: for odd n
else
DP[i] = DP[(i - 1) / 2] +
DP[(i + 1) / 2];
}
return DP[n];
}
// Driver program
int main()
{
int n = 15;
cout << findSDSFunc(n) << endl;
return 0;
}
Java
// Java program to find the nth element
// of Stern's Diatomic Series
class GFG {
// function to find nth stern'
// diatomic series
static int findSDSFunc(int n)
{
// Initializing the DP array
int DP[] = new int[n+1];
// SET the Base case
DP[0] = 0;
DP[1] = 1;
// Traversing the array from
// 2nd Element to nth Element
for (int i = 2; i <= n; i++)
{
// Case 1: for even n
if (i % 2 == 0)
DP[i] = DP[i / 2];
// Case 2: for odd n
else
DP[i] = DP[(i - 1) / 2] +
DP[(i + 1) / 2];
}
return DP[n];
}
// Driver program
public static void main(String[] args)
{
int n = 15;
System.out.println(findSDSFunc(n));
}
}
// This code is contributed by Smita Semwal.
Python 3
# Program to find the nth element
# of Stern's Diatomic Series
# function to find nth stern'
# diatomic series
def findSDSFunc(n):
# Initializing the DP array
DP = [0] * (n+1)
# SET the Base case
DP[0] = 0
DP[1] = 1
# Traversing the array from
# 2nd Element to nth Element
for i in range(2, n+1):
# Case 1: for even n
if (int(i % 2) == 0):
DP[i] = DP[int(i / 2)]
# Case 2: for odd n
else:
DP[i] = (DP[int((i - 1) / 2)]
+ DP[int((i + 1) / 2)])
return DP[n]
# Driver program
n = 15
print(findSDSFunc(n))
# This code is contribute by
# Smitha Dinesh Semwal
C#
// C# program to find the nth element
// of Stern's Diatomic Series
using System;
class GFG
{
// function to find nth
// stern' diatomic series
static int findSDSFunc(int n)
{
// Initializing the DP array
int []DP = new int[n + 1];
// SET the Base case
DP[0] = 0;
DP[1] = 1;
// Traversing the array from
// 2nd Element to nth Element
for (int i = 2; i <= n; i++)
{
// Case 1: for even n
if (i % 2 == 0)
DP[i] = DP[i / 2];
// Case 2: for odd n
else
DP[i] = DP[(i - 1) / 2] +
DP[(i + 1) / 2];
}
return DP[n];
}
// Driver Code
static public void Main ()
{
int n = 15;
Console.WriteLine(findSDSFunc(n));
}
}
// This code is contributed by aj_36
PHP
Javascript
输出:
4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。