给定整数n。我们必须找到斯特恩双原子级数的第n个项。
斯特恩的双原子序列是生成以下整数序列0、1、1、2、1、3、2、3、1、4、3、5、2、5、3、4,…的序列。它出现在Calkin-Wilf树中。有时也称为fusc函数。
用数学术语来说,斯特恩双原子序列的序列P(n)由递归关系定义。
例子 :
Input : n = 7
Output : 3
Input : n = 15
Output : 4
方法 :
我们使用非常简单的动态规划概念来解决此问题,该概念用于查找斐波那契数。保存DP [0] = 0,DP [1] = 1的基本情况之后,我们必须简单地从i = 2遍历到n并根据斯特恩双原子级数的解释定义计算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