📌  相关文章
📜  大于或等于N的最小数字,可被其非零数字整除

📅  最后修改于: 2021-05-06 18:50:58             🧑  作者: Mango

给定整数N ,任务是找到大于或等于N的最小数字,以使其可被其所有非零数字整除。

例子:

方法:最小数目其是通过从1到9所有数字整除等于的LCM(1,2,3,4,5,6,7,8,9)= 2520因此,2520的倍数也可从19的所有数字整除,这意味着(N + 2520)将始终满足该条件。因此,请在[N,2520 + N]范围内进行迭代,并检查满足给定条件的最小数字。请按照以下步骤解决问题:

  • ans初始化为0,以存储大于或等于N的最小数字,以使其可被其所有非零数字整除。
  • 使用变量i遍历[N,N + 2520]范围。
    • 可能的变量初始化为1,以检查当前数字i是否满足给定条件。
    • 获取i的所有非零数字,并检查i是否可被它们中的每一个整除。如果发现为真,则将可能的值更新为1 ,并将ans更新为i ,然后退出循环。
  • 完成上述步骤后,将ans的值打印为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the smallest number
// greater than or equal to N such that
// it is divisible by its non-zero digits
void findSmallestNumber(int n)
{
 
    // Iterate in range[N, N + 2520]
    for (int i = n; i <= (n + 2520); ++i) {
 
        // To check if the current number
        // satisfies the given condition
        bool possible = 1;
 
        // Store the number in a temporary
        // variable
        int temp = i;
 
        // Loop until temp > 0
        while (temp) {
 
            // Check only for non zero digits
            if (temp % 10 != 0) {
 
                // Extract the current digit
                int digit = temp % 10;
 
                // If number is divisible
                // by current digit or not
                if (i % digit != 0) {
 
                    // Otherwise, set
                    // possible to 0
                    possible = 0;
 
                    // Break out of the loop
                    break;
                }
            }
 
            // Divide by 10
            temp /= 10;
        }
 
        if (possible == 1) {
            cout << i;
            return;
        }
    }
}
 
// Driver Code
int main()
{
    int N = 31;
 
    // Function Call
    findSmallestNumber(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to find the smallest number
// greater than or equal to N such that
// it is divisible by its non-zero digits
static void findSmallestNumber(int n)
{
     
    // Iterate in range[N, N + 2520]
    for(int i = n; i <= (n + 2520); ++i)
    {
         
        // To check if the current number
        // satisfies the given condition
        int possible = 1;
  
        // Store the number in a temporary
        // variable
        int temp = i;
  
        // Loop until temp > 0
        while (temp != 0)
        {
             
            // Check only for non zero digits
            if (temp % 10 != 0)
            {
                 
                // Extract the current digit
                int digit = temp % 10;
  
                // If number is divisible
                // by current digit or not
                if (i % digit != 0)
                {
                     
                    // Otherwise, set
                    // possible to 0
                    possible = 0;
  
                    // Break out of the loop
                    break;
                }
            }
  
            // Divide by 10
            temp /= 10;
        }
  
        if (possible == 1)
        {
            System.out.println(i);
            return;
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int N = 31;
  
    // Function Call
    findSmallestNumber(N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to find the smallest number
# greater than or equal to N such that
# it is divisible by its non-zero digits
def findSmallestNumber(n):
 
    # Iterate in range[N, N + 2520]
    for i in range(n, n + 2521):
 
        # To check if the current number
        # satisfies the given condition
        possible = 1
 
        # Store the number in a temporary
        # variable
        temp = i
 
        # Loop until temp > 0
        while (temp):
 
            # Check only for non zero digits
            if (temp % 10 != 0):
 
                # Extract the current digit
                digit = temp % 10
 
                # If number is divisible
                # by current digit or not
                if (i % digit != 0):
 
                    # Otherwise, set
                    # possible to 0
                    possible = 0
 
                    # Break out of the loop
                    break
 
            # Divide by 10
            temp //= 10
 
        if (possible == 1):
            print(i, end = "")
            return
 
# Driver Code
if __name__ == "__main__" :
 
    N = 31
 
    # Function Call
    findSmallestNumber(N)
     
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the smallest number
// greater than or equal to N such that
// it is divisible by its non-zero digits
static void findSmallestNumber(int n)
{
     
    // Iterate in range[N, N + 2520]
    for(int i = n; i <= (n + 2520); ++i)
    {
         
        // To check if the current number
        // satisfies the given condition
        int possible = 1;
 
        // Store the number in a temporary
        // variable
        int temp = i;
 
        // Loop until temp > 0
        while (temp != 0)
        {
             
            // Check only for non zero digits
            if (temp % 10 != 0)
            {
                 
                // Extract the current digit
                int digit = temp % 10;
 
                // If number is divisible
                // by current digit or not
                if (i % digit != 0)
                {
                     
                    // Otherwise, set
                    // possible to 0
                    possible = 0;
 
                    // Break out of the loop
                    break;
                }
            }
 
            // Divide by 10
            temp /= 10;
        }
 
        if (possible == 1)
        {
            Console.Write(i);
            return;
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 31;
 
    // Function Call
    findSmallestNumber(N);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
33

时间复杂度: O(2520 * log 10 N)
辅助空间: O(1)