给定两个整数n和m,它们代表一个范围,其中n是下限,m是上限。任务是找到所有可能的数字,它们位于n和m之间,并可以除以其数字。
例子:
Input: n = 1, m = 15
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]
Explanation:
The numbers in the array can be divided evenly by its digits except
2 numbers 10 and 13 which lie between 1 and 15.
10 can be broken into its digits 1 and 0.
10 % 1 == 0 but 10 % 0 != 0.
13 can be broken into its digits 1 and 3.
13 % 1 == 0 but 13 % 3 != 0.
Input: n = 21, m = 25
Output: [22, 24]
Explanation:
The numbers in the array can be divided evenly by its digits except
there are 3 numbers 21, 23 and 25 lie between 21 and 25.
21 can be broken into its digits 2 and 1.
21 % 2 != 0 and 21 % 1 != 0
23 can be broken into its digits 2 and 3.
23 % 2 != 0 and 23 % 3 != 0.
25 can be broken into its digits 2 and 5.
25 % 2 != 0 but 25 % 5 == 0.
方法:
主要思想是从左到右迭代每个数字。对于每个数字,将其转换为字符串,后跟一个字符数组,并检查是否包含0(如果包含0)将其忽略。否则,对于每个数字,请检查数字是否被数字均分。如果数字除以数字,则将其添加到列表中,否则将其丢弃。最后,返回列表。
下面是上述方法的实现:
C++
// C++ program to find all the
// possible numbers that can be
// evenly divided by its digits
#include
#include
#include
using namespace std;
// Function to check whether the
// number is evenly divisible by
// its digits or not.
bool isDivisible(int num)
{
// Iterate each number convert
// number into string and then
// to character array.
// declaring output string stream
ostringstream str1;
// Sending a number as a stream
// into output string
str1 << num;
// the str() coverts number into
// string
string str2 = str1.str();
for (char c : str2 )
{
if (c == '0' ||
num % (c - '0') > 0)
{
return false;
}
}
return true;
}
// Function to check each and every
// number from left to right. If the
// number is divisible by its digits
// then the number is added into the list
vector selfDividingNumber(int left,
int right)
{
vectorlist ;
for (int i = left; i <= right; i++)
{
if (isDivisible(i))
{
list.push_back(i);
}
}
return list;
}
// Driver Code
int main()
{
// initialise range
int n1 = 1, m1 = 15;
vector ans =
(selfDividingNumber(n1, m1));
for(auto i = ans.begin();
i != ans.end(); i++)
cout << (*i) << " ";
}
// This code is contributed by Chitranayal
Java
// Java program to find all the possible numbers
// that can be evenly divided by its digits
import java.util.*;
class GFG {
// Function to check each and every number
// from left to right. If the number is
// divisible by its digits
// then the number is added into the list
static List selfDividingNumber(int left,
int right)
{
List list = new ArrayList();
for (int i = left; i <= right; i++) {
if (isDivisible(i)) {
list.add(i);
}
}
return list;
}
// Function to check whether the number
// is evenly divisible by its digits or not.
static boolean isDivisible(int num)
{
// Iterate each number convert number
// into string and then to character array.
for (char c : String.valueOf(num).toCharArray()) {
if (c == '0' || num % (c - '0') > 0) {
return false;
}
}
return true;
}
// Driver Code
public static void main(String args[])
{
// initialise range
int n1 = 1, m1 = 15;
System.out.print(selfDividingNumber(n1, m1));
}
}
Python3
# Python3 program to find all the possible numbers
# that can be evenly divided by its digits
# Function to check each and every number
# from left to right. If the number is
# divisible by its digits
# then the number is added into the list
def selfDividingNumber(left, right) :
array_list = [];
for i in range(left, right + 1) :
if (isDivisible(i)) :
array_list.append(i);
return array_list;
# Function to check whether the number
# is evenly divisible by its digits or not.
def isDivisible(num) :
# Iterate each number convert number
# into string and then to character array.
for c in list(str(num)) :
if (c == '0' or num % (ord(c) - ord('0')) > 0):
return False;
return True;
# Driver Code
if __name__ == "__main__" :
# Initialise range
n1 = 1; m1 = 15;
print(selfDividingNumber(n1, m1));
# This code is contributed by AnkitRai01
C#
// C# program to find all the
// possible numbers that can
// be evenly divided by its digits
using System;
using System.Collections.Generic;
public class GFG {
// Function to check each and every number
// from left to right. If the number is
// divisible by its digits then the number
// is added into the list
static List selfDividingNumber(int left,
int right)
{
List list = new List();
for(int i = left; i <= right; i++)
{
if (isDivisible(i))
{
list.Add(i);
}
}
return list;
}
// Function to check whether the number
// is evenly divisible by its digits or not.
static bool isDivisible(int num)
{
// Iterate each number convert number
// into string and then to character array.
foreach(char c in String.Join("", num).ToCharArray())
{
if (c == '0' || num % (c - '0') > 0)
{
return false;
}
}
return true;
}
// Driver Code
public static void Main(String []args)
{
// Initialise range
int n1 = 1, m1 = 15;
List t = selfDividingNumber(n1, m1);
foreach(int val in t)
Console.Write(val + ", ");
}
}
// This code is contributed by sapnasingh4991
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]
时间复杂度: O(N),其中N是从左到右的整数数。