📌  相关文章
📜  最小的x,使得1 * n,2 * n,…x * n的所有数字都从1到9

📅  最后修改于: 2021-05-04 08:51:08             🧑  作者: Mango

给定一个正数n。我们需要找到x,以便1 * n,2 * n,3 * n ….. x * n至少给出一次所有10位数字。如果没有,则打印-1。

例子:

Input : n = 1692
Output : 3
Explanation:
n = 1692, we got the digits- 1, 2, 6, 9
2*n = 3384, we got the digits- 1, 2, 3, 4, 
6, 8, 9.
3*n = 5076, we got the digits- 1, 2, 3, 4, 
5, 6, 7, 8, 9.
At this step we got all the digits at least
once. Therefore our answer is 3.

Input  : 1
Output : 10

Input  : 0
Output :-1

这里使用的想法很简单。我们从1开始,一直与n相乘,直到至少一次都没有得到所有10位数字。为了跟踪每次迭代中出现的所有数字,我们使用大小为10的临时数组,最初具有全零。每当我们第一次获得一个数字时,我们都会用1初始化它在数组中的索引。当所有数字都被访问一次时,我们就完成了。

以下是它的实现。

C++
// CPP program to find x such that 1*n, 2*n, 3*n
// ...x * n have all digits from 1 to 9 at least
// once
#include 
using namespace std;
  
// Returns smallest value x such that 1*n, 2*n, 
// 3*n ...x * n have all digits from 1 to 9 at 
// least once
int smallestX(int n)
{
    // taking temporary array and variable.
    int temp[10] = { 0 };
  
    if (n == 0)
        return -1;
  
    // iterate till we get all the 10 digits 
    // at least once
    int count = 0, x = 0;
    for (x = 1; count < 10; x++) {
        int y = x * n;
          
        // checking all the digits
        while (y) {
            if (temp[y % 10] == false) {
                count++;
                temp[y % 10] = true;
            }
            y /= 10;
        }
    }
    return x - 1;
}
  
// driver function
int main()
{
    int n = 5;
    cout <


Java
// Java program to find x such 
// that 1*n, 2*n, 3*n...x * n
// have all digits from 1 to 9
// at least once
import java.io.*;
import java.util.*;
  
class GFG
{
      
// Returns smallest value x 
// such that 1*n, 2*n, 3*n 
// ...x * n have all digits 
// from 1 to 9 at least once
public static int smallestX(int n)
{
    // taking temporary 
    // array and variable.
    int[] temp = new int[10];
    for(int i = 0; i < 10; i++)
    temp[i] = 0;
  
    if (n == 0)
        return -1;
  
    // iterate till we get 
    // all the 10 digits 
    // at least once
    int count = 0, x = 0;
    for (x = 1; count < 10; x++) 
    {
        int y = x * n;
          
        // checking all
        // the digits
        while (y > 0) 
        {
            if (temp[y % 10] == 0) 
            {
                count++;
                temp[y % 10] = 1;
            }
            y /= 10;
        }
    }
    return x - 1;
}
  
// Driver Code
public static void main(String args[])
{
    int n = 5;
    System.out.print(smallestX(n));
}
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python3 program to find x such 
# that 1*n, 2*n, 3*n ...x * n
# have all digits from 1 to 9 
# at least once
  
# Returns smallest value x such 
# that 1*n, 2*n, 3*n ...x * n 
# have all digits from 1 to 9 
# at least once
def smallestX(n):
    # taking temporary 
    # array and variable.
    temp = [0]*10
  
    if (n == 0):
        return -1
  
    # iterate till we get 
    # all the 10 digits 
    # at least once
    count = 0
    x = 1
    while(count < 10): 
        y = x * n
          
        # checking all
        # the digits
        while (y>0):
            if (temp[y % 10] == 0):
                count+=1
                temp[y % 10] = 1
            y = int(y / 10)
        x+=1
  
    return x - 1
  
  
# Driver code
if __name__=='__main__':
    n = 5
    print(smallestX(n))
  
# This code is contributed 
# by mits


C#
// C# program to find x such 
// that 1*n, 2*n, 3*n...x * n
// have all digits from 1 to 9
// at least once
using System;
  
class GFG
{
      
// Returns smallest value x 
// such that 1*n, 2*n, 3*n 
// ...x * n have all digits 
// from 1 to 9 at least once
public static int smallestX(int n)
{
    // taking temporary 
    // array and variable.
    int[] temp = new int[10];
    for(int i = 0; i < 10; i++)
    temp[i] = 0;
  
    if (n == 0)
        return -1;
  
    // iterate till we get 
    // all the 10 digits 
    // at least once
    int count = 0, x = 0;
    for (x = 1; count < 10; x++) 
    {
        int y = x * n;
          
        // checking all the digits
        while (y > 0) 
        {
            if (temp[y % 10] == 0) 
            {
                count++;
                temp[y % 10] = 1;
            }
            y /= 10;
        }
    }
    return x - 1;
}
  
// dDriver Code
static void Main()
{
    int n = 5;
    Console.Write(smallestX(n));
}
}
  
// This code is contributed by mits


PHP


输出:

18