📜  哥伦布序列|套装2

📅  最后修改于: 2021-05-04 11:57:29             🧑  作者: Mango

给定数字N。任务是找到Golomb序列的前N个项。 Golomb序列是一个非递减整数序列,其中第n个项等于n在序列中出现的次数。

方法:

  1. 用[1]初始化数组arr [] ,因为Golomb序列以1开头。
  2. 将最后一个索引的值存储在映射M中
  3. 对于Golomb序列,直到N
    • 初始化cnt = 0并映射。
    • 如果cnt不等于0,则Golomb序列中的当前元素是该序列中的前一个元素,并降低cnt。
    • 否则,Golomb序列中的当前元素等于1 +序列中的前一个元素,并且cnt被更新为Golomb序列中当前元素处map的值,并降低了cnt。
    • 使用Golomb序列中的当前值映射当前索引。
  4. 打印存储在数组arr []中的Golomb序列的所有元素。

下面是上述方法的实现:

C++
// C++ program to find the first
// N terms of Golomb Sequence
#include "bits/stdc++.h"
#define MAX 100001
using namespace std;
 
// Function to print the Golomb
// Sequence
void printGolombSequence(int N)
{
 
    // Initialise the array
    int arr[MAX];
 
    // Initialise the cnt to 0
    int cnt = 0;
 
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
 
    // Map to store the count of
    // current element in Golomb
    // Sequence
    map M;
 
    // Store the count of 2
    M[2] = 2;
 
    // Iterate over 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence
        // which is 1 + previous
        // element
        if (cnt == 0) {
            arr[i] = 1 + arr[i - 1];
            cnt = M[arr[i]];
            cnt--;
        }
 
        // Else the current element
        // is the previous element
        // in this Sequence
        else {
            arr[i] = arr[i - 1];
            cnt--;
        }
 
        // Map the current index to
        // current value in arr[]
        M[i] = arr[i];
    }
 
    // Print the Golomb Sequence
    for (int i = 1; i <= N; i++) {
        cout << arr[i] << ' ';
    }
}
 
// Driver Code
int main()
{
    int N = 11;
    printGolombSequence(N);
    return 0;
}


Java
// Java program to find the first
// N terms of Golomb Sequence
import java.util.*;
 
class GFG{
 
static int MAX = 1000;
 
// Function to print the Golomb
// Sequence
static void printGolombSequence(int N)
{
 
    // Initialise the array
    int []arr = new int[MAX];
    for(int i = 0; i < MAX; i++)
    arr[i] = 0;
     
    // Initialise the cnt to 0
    int cnt = 0;
 
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
 
    // Map to store the count of
    // current element in Golomb
    // Sequence
    Map M=new HashMap();
     
    // Store the count of 2
    M.put(2,2);
 
    // Iterate over 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence 1 2 2 3 3 4 4 4 5 5 5
        // which is 1 + previous
        // element
        if (cnt == 0) {
            arr[i] = 1 + arr[i - 1];
            cnt = M.get(arr[i]);
            cnt--;
        }
 
        // Else the current element
        // is the previous element
        // in this Sequence
        else {
            arr[i] = arr[i - 1];
            cnt--;
        }
 
        // Map the current index to
        // current value in arr[]
            M.put(i, arr[i]);
    }
 
    // Print the Golomb Sequence
    for (int i = 1; i <= N; i++)
    {
        System.out.print(arr[i]+" ");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int N = 11;
    printGolombSequence(N);
}
}
 
// This code is contributed by Surendra_Gangwar


Python3
# Python3 program to find the first
# N terms of Golomb Sequence
MAX = 100001
 
# Function to print the Golomb
# Sequence
def printGolombSequence(N):
 
    # Initialise the array
    arr = [0] * MAX
 
    # Initialise the cnt to 0
    cnt = 0
 
    # First and second element
    # of Golomb Sequence is 0, 1
    arr[0] = 0
    arr[1] = 1
 
    # Map to store the count of
    # current element in Golomb
    # Sequence
    M = dict()
 
    # Store the count of 2
    M[2] = 2
 
    # Iterate over 2 to N
    for i in range(2, N + 1):
 
        # If cnt is equals to 0
        # then we have new number
        # for Golomb Sequence
        # which is 1 + previous
        # element
        if (cnt == 0):
            arr[i] = 1 + arr[i - 1]
            cnt = M[arr[i]]
            cnt -= 1
 
        # Else the current element
        # is the previous element
        # in this Sequence
        else:
            arr[i] = arr[i - 1]
            cnt -= 1
 
        # Map the current index to
        # current value in arr[]
        M[i] = arr[i]
 
    # Print the Golomb Sequence
    for i in range(1, N + 1):
        print(arr[i], end=" ")
 
# Driver Code
N = 11
printGolombSequence(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the first
// N terms of Golomb Sequence
using System;
using System.Collections.Generic;
 
class GFG{
     
static int MAX = 1000;
 
// Function to print the Golomb
// Sequence
static void printGolombSequence(int N)
{
     
    // Initialise the array
    int[] arr = new int[MAX];
    for(int i = 0; i < MAX; i++)
        arr[i] = 0;
       
    // Initialise the cnt to 0
    int cnt = 0;
   
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
   
    // Map to store the count of
    // current element in Golomb
    // Sequence
    Dictionary M = new Dictionary(); 
       
    // Store the count of 2
    M.Add(2, 2);
   
    // Iterate over 2 to N
    for(int i = 2; i <= N; i++)
    {
         
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence 1 2 2 3 3 4 4 4 5 5 5
        // which is 1 + previous
        // element
        if (cnt == 0)
        {
            arr[i] = 1 + arr[i - 1];
            cnt = M[arr[i]];
            cnt--;
        }
   
        // Else the current element
        // is the previous element
        // in this Sequence
        else
        {
            arr[i] = arr[i - 1];
            cnt--;
        }
   
        // Map the current index to
        // current value in arr[]
        if(M.ContainsKey(i))
        {
            M[i] = arr[i];
        }
        else
        {
            M.Add(i, arr[i]);
        }
    }
   
    // Print the Golomb Sequence
    for(int i = 1; i <= N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver Code
static void Main()
{
    int N = 11;
     
    printGolombSequence(N);
}
}
 
// This code is contributed by divyeshrabadiya07


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

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