📜  多个种子(或种子根)

📅  最后修改于: 2021-04-23 19:36:33             🧑  作者: Mango

数字n的种子是数字x,因此x与它的数字的乘积等于n。任务是找到给定数字n的所有种子。如果不存在种子,则打印相同的种子。

例子:

Input  : n = 138
Output : 23 
23 is a seed of 138 because
23*2*3 is equal to 138

Input : n = 4977
Output : 79 711 
79 is a seed of 4977 because
79 * 7 * 9 = 4977.
711 is also a seed of 4977 because
711 * 1 * 1 * 7 = 4977

Input  : n = 9
Output : No seed exists

Input  : n = 738
Output : 123 


在史诗中问

这个想法是遍历从1到n / 2的所有数字。对于每个要遍历的数字,请找到数字与该数字的乘积。在下面的程序中完成的一个重要优化是避免重新计算数字产品。我们将产品存储在一个阵列中。如果已经计算出产品,则将其返回,否则将对其进行计算。

以下是该想法的实现。

C++
// C++ program to find Seed of a number
#include 
using namespace std;
const int MAX = 10000;
  
int prodDig[MAX];
  
// Stores product of digits of x in prodDig[x]
int getDigitProduct(int x)
{
    // If x has single digit
    if (x < 10)
      return x;
  
    // If digit product is already computed
    if (prodDig[x] != 0)
       return prodDig[x];
  
    // If digit product is not computed before.
    int prod = (x % 10) * getDigitProduct(x/10);
  
    return (prodDig[x] = prod);
}
  
// Prints all seeds of n
void findSeed(int n)
{
    // Find all seeds using prodDig[]
    vector res;
    for (int i=1; i<=n/2; i++)
        if (i*getDigitProduct(i) == n)
            res.push_back(i);
  
    // If there was no seed
    if (res.size() == 0)
    {
        cout << "NO seed exists\n";
        return;
    }
  
    // Print seeds
    for (int i=0; i


Java
// Java program to find Seed of a number
import java.util.*; 
  
class GFg{
static int MAX = 10000;
static int[] prodDig=new int[MAX];
  
// Stores product of digits of x in prodDig[x]
static int getDigitProduct(int x)
{
    // If x has single digit
    if (x < 10)
    return x;
  
    // If digit product is already computed
    if (prodDig[x] != 0)
    return prodDig[x];
  
    // If digit product is not computed before.
    int prod = (x % 10) * getDigitProduct(x/10);
  
    return (prodDig[x] = prod);
}
  
// Prints all seeds of n
static void findSeed(int n)
{
    // Find all seeds using prodDig[]
    List res = new ArrayList(); 
    for (int i=1; i<=n/2; i++)
        if (i*getDigitProduct(i) == n)
            res.add(i);
  
    // If there was no seed
    if (res.size() == 0)
    {
        System.out.println("NO seed exists");
        return;
    }
  
    // Print seeds
    for (int i=0; i


Python3
# Python3 program to find Seed of a number
  
MAX = 10000;
  
prodDig = [0] * MAX;
  
# Stores product of digits of 
# x in prodDig[x]
def getDigitProduct(x):
      
    # If x has single digit
    if (x < 10):
        return x;
  
    # If digit product is already computed
    if (prodDig[x] != 0):
        return prodDig[x];
  
    # If digit product is not computed before.
    prod = (int(x % 10) * 
            getDigitProduct(int(x / 10)));
  
    prodDig[x] = prod;
    return prod;
  
# Prints all seeds of n
def findSeed(n):
  
    # Find all seeds using prodDig[]
    res = [];
    for i in range(1, int(n / 2 + 2)):
        if (i * getDigitProduct(i) == n):
            res.append(i);
  
    # If there was no seed
    if (len(res) == 0):
        print("NO seed exists");
        return;
  
    # Print seeds
    for i in range(len(res)):
        print(res[i], end = " ");
  
# Driver code
n = 138;
findSeed(n);
  
# This code is contributed by mits


C#
// C# program to find Seed of a number
using System;
using System.Collections; 
  
class GFG{
      
static int MAX = 10000;
static int[] prodDig=new int[MAX];
  
// Stores product of digits of x in prodDig[x]
static int getDigitProduct(int x)
{
    // If x has single digit
    if (x < 10)
    return x;
  
    // If digit product is already computed
    if (prodDig[x] != 0)
    return prodDig[x];
  
    // If digit product is not computed before.
    int prod = (x % 10) * getDigitProduct(x/10);
  
    return (prodDig[x] = prod);
}
  
// Prints all seeds of n
static void findSeed(int n)
{
    // Find all seeds using prodDig[]
    ArrayList res = new ArrayList(); 
    for (int i=1; i<=n/2; i++)
        if (i*getDigitProduct(i) == n)
            res.Add(i);
  
    // If there was no seed
    if (res.Count == 0)
    {
        Console.WriteLine("NO seed exists");
        return;
    }
  
    // Print seeds
    for (int i=0; i


PHP


输出 :

23

进一步优化:
我们可以进一步优化上面的代码。这个想法是仅当i被n整除时才调用getDigitProduct(i)。请参阅https://ide.geeksforgeeks.org/oLYduu进行实施。