给定一个整数N ,任务是找到大于或等于N的最小数,使得它可以被其所有非零数字整除。
例子:
Input: N = 31
Output: 33
Explanation: 33 is the smallest number satisfying the given condition.
At Unit’s place: 33%3 = 0
At One’s place: 33%3 = 0
Input: N = 30
Output: 30
Explanation: 30 is the smallest number satisfying the given condition.
At One’s place: 30%3 = 0
方法:最小数目其是通过从1到9所有数字整除等于的LCM(1,2,3,4,5,6,7,8,9)= 2520因此,2520的倍数也可以被1到9的所有数字整除,这意味着(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)