📜  哥伦布序列

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

在数学中,哥伦布序列是一个非递减的整数序列,其中第 n 项等于 n 在序列中出现的次数。
前几个值是
1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, ……
几个术语的解释:
第三项是 2,注意三出现了 2 次。
第二项是 2,注意两个出现了 2 次。
第四项是 3,注意 4 出现了 3 次。
给定一个正整数 n。任务是找到 Golomb 序列的前 n 项。
例子 :

Input : n = 4
Output : 1 2 2 3

Input : n = 6
Output : 1 2 2 3 3 4

求哥伦布序列第n项的递推关系:
a(1) = 1
a(n + 1) = 1 + a(n + 1 – a(a(n)))
下面是使用递归的实现:

C++
// C++ Program to find first
// n terms of Golomb sequence.
#include 
using namespace std;
 
// Return the nth element
// of Golomb sequence
int findGolomb(int n)
{
    // base case
    if (n == 1)
        return 1;
 
    // Recursive Step
    return 1 + findGolomb(n -
               findGolomb(findGolomb(n - 1)));
}
 
// Print the first n
// term of Golomb Sequence
void printGolomb(int n)
{
    // Finding first n
    // terms of Golomb Sequence.
    for (int i = 1; i <= n; i++)
        cout << findGolomb(i) << " ";
}
 
// Driver Code
int main()
{
    int n = 9;
    printGolomb(n);
    return 0;
}


Java
// Java Program to find first 
// n terms of Golomb sequence.
import java.util.*;
 
class GFG
{
    public static int findGolomb(int n)
    {
         
        // base case
        if (n == 1)
            return 1;
     
        // Recursive Step
        return 1 + findGolomb(n -
        findGolomb(findGolomb(n - 1)));
    }
     
     
    // Print the first n term of
    // Golomb Sequence
    public static void printGolomb(int n)
    {
         
        // Finding first n terms of
        // Golomb Sequence.
        for (int i = 1; i <= n; i++)
            System.out.print(findGolomb(i) +
                                       " ");
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 9;
         
        printGolomb(n);
    }
}
 
// This code is conttributed by Akash Singh


Python 3
# Python 3 Program to find first
# n terms of Golomb sequence.
 
# Return the nth element of
# Golomb sequence
def findGolomb(n):
 
    # base case
    if (n == 1):
        return 1
 
    # Recursive Step
    return 1 + findGolomb(n -
    findGolomb(findGolomb(n - 1)))
 
 
# Print the first n term
# of Golomb Sequence
def printGolomb(n):
 
    # Finding first n terms of
    # Golomb Sequence.
    for i in range(1, n + 1):
        print(findGolomb(i), end=" ")
 
# Driver Code
n = 9
 
printGolomb(n)
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# Program to find first n 
// terms of Golomb sequence.
using System;
 
class GFG
{
     
    // Return the nth element
    // of Golomb sequence
    static int findGolomb(int n)
    {
         
        // base case
        if (n == 1)
            return 1;
     
        // Recursive Step
        return 1 + findGolomb(n -
        findGolomb(findGolomb(n - 1)));
    }
     
    // Print the first n term 
    // of Golomb Sequence
    static void printGolomb(int n)
    {
        // Finding first n terms of
        // Golomb Sequence.
        for (int i = 1; i <= n; i++)
            Console .Write(findGolomb(i) +
                                     " ");
    }
     
    // Driver Code
    public static void Main ()
    {
         
        int n = 9;
         
        printGolomb(n);
         
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// C++ Program to find first
// n terms of Golomb sequence.
#include 
using namespace std;
 
// Print the first n term
// of Golomb Sequence
void printGolomb(int n)
{
    int dp[n + 1];
 
    // base cases
    dp[1] = 1;
    cout << dp[1] << " ";
 
    // Finding and printing first
    // n terms of Golomb Sequence.
    for (int i = 2; i <= n; i++)
    {
        dp[i] = 1 + dp[i - dp[dp[i - 1]]];
        cout << dp[i] << " ";
    }
}
// Driver Code
int main()
{
    int n = 9;
 
    printGolomb(n);
    return 0;
}


Java
// Java Program to find first
// n terms of Golomb sequence.
import java.util.*;
 
class GFG
{
     
    public static void printGolomb(int n)
    {
        int dp[] = new int[n + 1];
     
        // base cases
        dp[1] = 1;
        System.out.print(dp[1] + " ");
     
        // Finding and printing first n
        // terms of Golomb Sequence.
        for (int i = 2; i <= n; i++)
        {
            dp[i] = 1 + dp[i - dp[dp[i - 1]]];
             
        System.out.print(dp[i] + " ");
        }
    }
         
    // Driver code
    public static void main (String[] args)
    {
        int n = 9;
         
        printGolomb(n);
    }
}
 
// This code is contributed by Akash Singh


Python 3
# Python3 Program to find first
# n terms of Golomb sequence.
 
# Print the first n term
# of Golomb Sequence
def Golomb( n):
 
    dp = [0] * (n + 1)
 
    # base cases
    dp[1] = 1
    print(dp[1], end = " " )
 
    # Finding and pring first
    # n terms of Golomb Sequence.
    for i in range(2, n + 1):
     
        dp[i] = 1 + dp[i - dp[dp[i - 1]]]
        print(dp[i], end = " ")
     
# Driver Code
n = 9
 
Golomb(n)
 
# This code is contributed by ash264


C#
// C# Program to find first n
// terms of Golomb sequence.
using System;
 
class GFG
{
     
    // Print the first n term of
    // Golomb Sequence
    static void printGolomb(int n)
    {
        int []dp = new int[n + 1];
     
        // base cases
        dp[1] = 1;
        Console.Write(dp[1] + " ");
     
        // Finding and printing first n
        // terms of Golomb Sequence.
        for (int i = 2; i <= n; i++)
        {
            dp[i] = 1 + dp[i - dp[dp[i - 1]]];
            Console.Write( dp[i] + " ");
        }
    }
     
    // Driver Code
    public static void Main ()
    {
         
        int n = 9;
         
        printGolomb(n);
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出 :
1 2 2 3 3 4 4 4 5

下面是使用动态规划的实现:

C++

// C++ Program to find first
// n terms of Golomb sequence.
#include 
using namespace std;
 
// Print the first n term
// of Golomb Sequence
void printGolomb(int n)
{
    int dp[n + 1];
 
    // base cases
    dp[1] = 1;
    cout << dp[1] << " ";
 
    // Finding and printing first
    // n terms of Golomb Sequence.
    for (int i = 2; i <= n; i++)
    {
        dp[i] = 1 + dp[i - dp[dp[i - 1]]];
        cout << dp[i] << " ";
    }
}
// Driver Code
int main()
{
    int n = 9;
 
    printGolomb(n);
    return 0;
}

Java

// Java Program to find first
// n terms of Golomb sequence.
import java.util.*;
 
class GFG
{
     
    public static void printGolomb(int n)
    {
        int dp[] = new int[n + 1];
     
        // base cases
        dp[1] = 1;
        System.out.print(dp[1] + " ");
     
        // Finding and printing first n
        // terms of Golomb Sequence.
        for (int i = 2; i <= n; i++)
        {
            dp[i] = 1 + dp[i - dp[dp[i - 1]]];
             
        System.out.print(dp[i] + " ");
        }
    }
         
    // Driver code
    public static void main (String[] args)
    {
        int n = 9;
         
        printGolomb(n);
    }
}
 
// This code is contributed by Akash Singh

Python3

# Python3 Program to find first
# n terms of Golomb sequence.
 
# Print the first n term
# of Golomb Sequence
def Golomb( n):
 
    dp = [0] * (n + 1)
 
    # base cases
    dp[1] = 1
    print(dp[1], end = " " )
 
    # Finding and pring first
    # n terms of Golomb Sequence.
    for i in range(2, n + 1):
     
        dp[i] = 1 + dp[i - dp[dp[i - 1]]]
        print(dp[i], end = " ")
     
# Driver Code
n = 9
 
Golomb(n)
 
# This code is contributed by ash264

C#

// C# Program to find first n
// terms of Golomb sequence.
using System;
 
class GFG
{
     
    // Print the first n term of
    // Golomb Sequence
    static void printGolomb(int n)
    {
        int []dp = new int[n + 1];
     
        // base cases
        dp[1] = 1;
        Console.Write(dp[1] + " ");
     
        // Finding and printing first n
        // terms of Golomb Sequence.
        for (int i = 2; i <= n; i++)
        {
            dp[i] = 1 + dp[i - dp[dp[i - 1]]];
            Console.Write( dp[i] + " ");
        }
    }
     
    // Driver Code
    public static void Main ()
    {
         
        int n = 9;
         
        printGolomb(n);
    }
}
 
// This code is contributed by vt_m

PHP


Javascript


输出 :
1 2 2 3 3 4 4 4 5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程