📌  相关文章
📜  在可以除以数字的范围内找到所有可能的数字

📅  最后修改于: 2021-04-29 01:01:37             🧑  作者: Mango

给定两个整数n和m,它们代表一个范围,其中n是下限,m是上限。任务是找到所有可能的数字,它们位于n和m之间,并可以除以其数字。
例子:

方法:
主要思想是从左到右迭代每个数字。对于每个数字,将其转换为字符串,后跟一个字符数组,并检查是否包含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是从左到右的整数数。