📌  相关文章
📜  检查N是否可被包含{A,B}中的数字组成的数字整除

📅  最后修改于: 2021-04-22 04:08:13             🧑  作者: Mango

给定三个整数NAB ,任务是查找N是否可被任何仅包含AB作为数字的数字整除。

例子:

方法1(递归):一种有效的解决方案是使用从数字ab开始的递归函数,将所有包含ab的数字作为其数字。如果函数调用为fun(x),则递归调用fun(x * 10 + a)fun(x * 10 + b) 。如果n可被任何数字整除,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++
// CPP program to find if number N is divisible by a
// number that contains only a and b as it's digits
#include 
using namespace std;
  
// Function to check whether n is divisible 
// by a number whose digits are either a or b
bool isDivisibleRec(int x, int a, int b, int n)
{
    // base condition
    if (x > n)
        return false;
  
    if (n % x == 0)
        return true;
  
    // recursive call
    return (isDivisibleRec(x * 10 + a, a, b, n)
            || isDivisibleRec(x * 10 + b, a, b, n));
}
  
bool isDivisible(int a, int b, int n)
{
   // Check for all numbers beginning with 'a' or 'b'
   return isDivisibleRec(a, a, b, n) || 
          isDivisibleRec(b, a, b, n);
}
  
// Driver program
int main()
{
    int a = 3, b = 5, n = 53;
  
    if (isDivisible(a, b, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to find if number N is divisible by a
// number that contains only a and b as it's digits
  
import java.util.*;
class solution
{
  
// Function to check whether n is divisible 
// by a number whose digits are either a or b
static boolean isDivisibleRec(int x, int a, int b, int n)
{
    // base condition
    if (x > n)
        return false;
  
    if (n % x == 0)
        return true;
  
    // recursive call
    return (isDivisibleRec(x * 10 + a, a, b, n) 
            || isDivisibleRec(x * 10 + b, a, b, n));
}
  
static boolean isDivisible(int a, int b, int n)
{
// Check for all numbers beginning with 'a' or 'b'
return isDivisibleRec(a, a, b, n) 
        ||isDivisibleRec(b, a, b, n);
}
  
// Driver program
public static void main(String args[])
{
    int a = 3, b = 5, n = 53;
  
    if (isDivisible(a, b, n))
        System.out.print("Yes");
    else
        System.out.print("No");
  
}
  
}
//contributed by Arnab Kundu


Python 3
# Python 3 program to find if number N 
# is divisible by a number that contains 
# only a and b as it's digits
  
# Function to check whether n is divisible 
# by a number whose digits are either a or b
def isDivisibleRec(x, a, b, n):
  
    # base condition
    if (x > n):
        return False
  
    if (n % x == 0):
        return True
  
    # recursive call
    return (isDivisibleRec(x * 10 + a, a, b, n) or 
            isDivisibleRec(x * 10 + b, a, b, n))
  
def isDivisible(a, b, n):
  
    # Check for all numbers beginning
    # with 'a' or 'b'
    return (isDivisibleRec(a, a, b, n) or 
            isDivisibleRec(b, a, b, n))
  
# Driver Code
a = 3; b = 5; n = 53;
  
if (isDivisible(a, b, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed 
# by Akanksha Rai


C#
// C# program to find if number N is 
// divisible by a number that contains
// only a and b as it's digits 
using System;
  
class GFG
{
      
// Function to check whether n is divisible 
// by a number whose digits are either a or b 
static bool isDivisibleRec(int x, int a, 
                           int b, int n) 
{ 
    // base condition 
    if (x > n) 
        return false; 
  
    if (n % x == 0) 
        return true; 
  
    // recursive call 
    return (isDivisibleRec(x * 10 + a, a, b, n) ||
            isDivisibleRec(x * 10 + b, a, b, n)); 
} 
  
static bool isDivisible(int a, int b, int n) 
{ 
      
// Check for all numbers beginning
// with 'a' or 'b' 
return isDivisibleRec(a, a, b, n) || 
       isDivisibleRec(b, a, b, n); 
} 
  
// Driver Code
static public void Main ()
{
    int a = 3, b = 5, n = 53; 
  
    if (isDivisible(a, b, n)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No"); 
} 
} 
  
// This code is contributed by Sachin


PHP
 $n)
        return false;
  
    if ($n % $x == 0)
        return true;
  
    // recursive call
    return (isDivisibleRec($x * 10 + $a, $a, $b, $n) ||
            isDivisibleRec($x * 10 + $b, $a, $b, $n));
}
  
function isDivisible($a, $b, $n)
{
      
// Check for all numbers beginning 
// with 'a' or 'b'
return isDivisibleRec($a, $a, $b, $n) || 
       isDivisibleRec($b, $a, $b, $n);
}
  
// Driver Code
$a = 3; $b = 5; $n = 53;
  
if (isDivisible($a, $b, $n))
    echo "Yes";
else
    echo "No";
  
// This code is contributed
// by Akanksha Rai


输出:
Yes

方法2(基于队列):想法是生成包含数字a和b的所有数字(小于n)。对于每个数字,请检查其是否除以n。如何生成小于n的所有数字?我们为此使用队列。最初,我们将“ a”和“ b”推入队列。然后,当队列的前面小于n时,我们运行一个循环。我们一个接一个地弹出一个项,对于永远弹出的项x,我们生成下一个数字x * 10 + a和x * 10 + b并将它们排队。该方法的时间复杂度为O(n)

请参阅以下帖子以了解此方法的实现。
小于N的二进制数字计数