📌  相关文章
📜  在给定的一组因子下生成n个数字

📅  最后修改于: 2021-05-04 12:37:03             🧑  作者: Mango

给定一个k个数字factor []数组,任务是打印第n个数字(按升序排列),这些数字来自给定数组。

例子:

Input  : factor[] = {2, 3, 4, 7} 
         n = 8
Output : 2 3 4 6 7 8 9 10

Input  :  factor[] = {3, 5, 7}
          n = 10
Output :  3 5 6 7 9 10 12 14 15 18

此问题主要是“丑数问题”的扩展。我们创建一个辅助数组next [] ,其大小与factor []相同,以跟踪因子的下一个倍数。在每个迭代中,我们从下一个输出最小元素,然后将其增加各自的因数。如果最小值等于先前的输出值,则将其递增(以避免重复)。否则,我们将打印最小值。

C/C++
// C++ program to generate n numbers with
// given factors
#include
using namespace std;
  
// Generate n numbers with factors in factor[]
void generateNumbers(int factor[], int n, int k)
{
    // array of k to store next multiples of
    // given factors 
    int next[k] = {0};
  
    // Prints n numbers  
    int output = 0;  // Next number to print as output 
    for (int i=0; i


Java
// Java program to generate
// n numbers with given factors
import java.io.*;
  
class GFG 
{
      
// Generate n numbers with
// factors in factor[]
static void generateNumbers(int factor[],
                            int n, int k)
{
    // array of k to store 
    // next multiples of 
    // given factors 
    int next[] = new int[k];
  
    // Prints n numbers 
    int output = 0; // Next number to
                    // print as output 
    for (int i = 0; i < n;)
    {
        // Find the next 
        // smallest multiple
        int toincrement = 0;
        for (int j = 0; j < k; j++)
            if (next[j] < next[toincrement])
                toincrement = j;
  
        // Printing minimum in 
        // each iteration print 
        // the value if output 
        // is not equal to current
        // value(to avoid the 
        // duplicates)
        if (output != next[toincrement])
        {
            output = next[toincrement];
            System.out.print(
                   next[toincrement] + " ");
            i++;
        }
  
        // incrementing the 
        // current value by the
        // respective factor
        next[toincrement] += 
               factor[toincrement];
    }
}
  
// Driver code
public static void main (String[] args) 
{
    int factor[] = {3, 5, 7};
    int n = 10;
    int k = factor.length;
    generateNumbers(factor, n, k);
}
}
  
// This code is contributed 
// by ajit


Python3
# Python3 program to generate n 
# numbers with given factors
  
# Generate n numbers with 
# factors in factor[]
def generateNumbers(factor, n, k):
      
    # array of k to store next 
    # multiples of given factors 
    next = [0] * k;
  
    # Prints n numbers 
    output = 0; # Next number to
                # print as output 
    i = 0;
    while(i < n):
          
        # Find the next smallest 
        # multiple
        toincrement = 0;
        for j in range(k):
            if(next[j] < next[toincrement]):
                toincrement = j;
  
        # Printing minimum in each 
        # iteration print the value
        # if output is not equal to
        # current value(to avoid the
        # duplicates)
        if(output != next[toincrement]):
            output = next[toincrement];
            print(next[toincrement], end = " ");
            i += 1;
  
        # incrementing the current 
        # value by the respective factor
        next[toincrement] += factor[toincrement];
  
# Driver code
factor = [3, 5, 7];
n = 10;
k = len(factor);
generateNumbers(factor, n, k);
      
# This code is contributed by mits


C#
// C# program to generate
// n numbers with given factors
using System;
  
class GFG
{
      
// Generate n numbers with
// factors in factor[]
static void generateNumbers(int []factor,
                            int n, int k)
{
    // array of k to store 
    // next multiples of 
    // given factors 
    int []next = new int[k];
  
    // Prints n numbers 
    int output = 0; // Next number to
                    // print as output 
    for (int i = 0; i < n;)
    {
        // Find the next 
        // smallest multiple
        int toincrement = 0;
        for (int j = 0; j < k; j++)
            if (next[j] < next[toincrement])
                toincrement = j;
  
        // Printing minimum in 
        // each iteration print 
        // the value if output 
        // is not equal to current
        // value(to avoid the 
        // duplicates)
        if (output != next[toincrement])
        {
            output = next[toincrement];
            Console.Write(
                next[toincrement] + " ");
            i++;
        }
  
        // incrementing the 
        // current value by the
        // respective factor
        next[toincrement] += 
            factor[toincrement];
    }
}
  
// Driver code
static public void Main ()
{
    int []factor = {3, 5, 7};
    int n = 10;
    int k = factor.Length;
    generateNumbers(factor, n, k);
}
}
  
// This code is contributed 
// by akt_mit


PHP


输出 :

3 5 6 7 9 10 12 14 15 18 

时间复杂度– O(n * k)
辅助空间– O(k)