如果Lynch-Bell数位的所有数字均互不相同,并且每个数字均可以整除N,则该数字为N。
1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24…
给定数字N ,任务是检查N是否是Lynch-Bell号码。如果N是Lynch-Bell号码,则打印“是”,否则打印“否” 。
例子:
Input: N = 384
Output: Yes
Explanation:
384/3 = 128, 384/8 = 48, 384/4 = 96
and 384 has all distinct digits.
Input: N = 1123
Output: No
Explanation:
方法:
- 想法是遍历给定数字的每个数字,并将遍历的数字标记为已访问。由于数字的总数是10,因此我们需要一个大小仅为10的布尔数组来标记访问的数字。
- 我们要测试每个数字是否为非零并除以数字。例如,对于128,我们要测试d!= 0 && 128%d == 0(对于d = 1、2、8)。为此,我们需要遍历数字的每个数字。
- 然后,如果其数字都是唯一的,并且N可以被每个数字整除,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation for the
// above approach
#include
using namespace std;
// Function to check the divisibility
// of the number by its digit.
bool checkDivisibility(int n, int digit)
{
// If the digit divides the number
// then return true else return false.
return (digit != 0 && n % digit == 0);
}
// Function to check if all digits
// of n divide it or not
bool isAllDigitsDivide(int n)
{
int temp = n;
while (temp > 0) {
// Taking the digit of the
// number into digit var.
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false;
temp /= 10;
}
return true;
}
// Function to check if N
// has all distinct digits
bool isAllDigitsDistinct(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
bool arr[10];
for (int i = 0; i < 10; i++)
arr[i] = false;
// Traverse through all digits of given number
while (n > 0) {
// Find the last digit
int digit = n % 10;
// If digit is already seen, return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// REmove the last digit from number
n = n / 10;
}
return true;
}
// Function to check Lynch-Bell numbers
bool isLynchBell(int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
// Driver Code
int main()
{
// Given Number N
int N = 12;
// Function Call
if (isLynchBell(N))
cout << "Yes";
else
cout << "No";
return 0;
}
C
// C implementation for the
// above approach
#include
// Function to check the divisibility
// of the number by its digit.
int checkDivisibility(int n, int digit)
{
// If the digit divides the number
// then return true else return false.
return (digit != 0 && n % digit == 0);
}
// Function to check if all digits
// of n divide it or not
int isAllDigitsDivide(int n)
{
int temp = n;
while (temp > 0)
{
// Taking the digit of the
// number into digit var.
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return 0;
temp /= 10;
}
return 1;
}
// Function to check if N
// has all distinct digits
int isAllDigitsDistinct(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
int arr[10],i,digit;
for(i = 0; i < 10; i++)
arr[i] = 0;
// Traverse through all digits of given number
while (n > 0)
{
// Find the last digit
digit = n % 10;
// If digit is already seen, return false
if (arr[digit])
return 0;
// Mark this digit as seen
arr[digit] = 1;
// Remove the last digit from number
n = n / 10;
}
return 1;
}
// Function to check Lynch-Bell numbers
int isLynchBell(int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
// Driver Code
int main()
{
// Given number N
int N = 12;
// Function call
if (isLynchBell(N))
printf("Yes");
else
printf("No");
return 0;
}
// This code is contributed by adityakumar27200
Java
// Java program for above approach
class GFG{
// Function to check the divisibility
// of the number by its digit.
static boolean checkDivisibility(int n,
int digit)
{
// If the digit divides the number
// then return true else return false.
return (digit != 0 && n % digit == 0);
}
// Function to check if all digits
// of n divide it or not
static boolean isAllDigitsDivide(int n)
{
int temp = n;
while (temp > 0)
{
// Taking the digit of the
// number into digit var.
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false;
temp /= 10;
}
return true;
}
// Function to check if N
// has all distinct digits
static boolean isAllDigitsDistinct(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
boolean arr[] = new boolean[10];
// Traverse through all digits of given number
while (n > 0)
{
// Find the last digit
int digit = n % 10;
// If digit is already seen, return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// REmove the last digit from number
n = n / 10;
}
return true;
}
// Function to check Lynch-Bell numbers
static boolean isLynchBell(int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
// Driver Code
public static void main(String[] args)
{
// Given Number N
int N = 12;
// Function Call
if (isLynchBell(N))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Shubham Prakash
Python3
# Python3 implementation for the
# above approach
import math
# Function to check the divisibility
# of the number by its digit.
def checkDivisibility(n, digit):
# If the digit divides the number
# then return true else return false.
return ((digit != 0) and ((n % digit) == 0))
# Function to check if all digits
# of n divide it or not
def isAllDigitsDivide(n):
temp = n
while (temp >= 1):
# Taking the digit of the number
# into digit var.
digit = int(temp % 10)
if (checkDivisibility(n, digit) == False):
return 0
temp = temp / 10
return 1
# Function to check if N has all
# distinct digits
def isAllDigitsDistinct(n):
# Create an array of size 10 and
# initialize all elements as false.
# This array is used to check if a
# digit is already seen or not.
arr = [0] * 10
# Traverse through all digits
# of given number
while (n >= 1):
# Find the last digit
digit = int(n % 10)
# If digit is already seen, return false
if(arr[digit]):
return 0
# Mark this digit as seen
arr[digit] = 1
# Remove the last digit from number
n = int(n / 10)
return 1
# Function to check Lynch-Bell numbers
def isLynchBell(n):
return (isAllDigitsDivide(n) and
isAllDigitsDistinct(n))
# Driver Code
if __name__=='__main__':
# Given number N
N = 12
# Function call
if isLynchBell(N):
print("Yes")
else:
print("No")
# This code is contributed by adityakumar27200
C#
// C# program for above approach
using System;
class GFG{
// Function to check the divisibility
// of the number by its digit.
static bool checkDivisibility(int n,
int digit)
{
// If the digit divides the number
// then return true else return false.
return (digit != 0 && n % digit == 0);
}
// Function to check if all digits
// of n divide it or not
static bool isAllDigitsDivide(int n)
{
int temp = n;
while (temp > 0)
{
// Taking the digit of the
// number into digit var.
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false;
temp /= 10;
}
return true;
}
// Function to check if N
// has all distinct digits
static bool isAllDigitsDistinct(int n)
{
// Create an array of size 10 and initialize all
// elements as false. This array is used to check
// if a digit is already seen or not.
bool []arr = new bool[10];
// Traverse through all digits of given number
while (n > 0)
{
// Find the last digit
int digit = n % 10;
// If digit is already seen, return false
if (arr[digit])
return false;
// Mark this digit as seen
arr[digit] = true;
// REmove the last digit from number
n = n / 10;
}
return true;
}
// Function to check Lynch-Bell numbers
static bool isLynchBell(int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
// Driver Code
public static void Main()
{
// Given Number N
int N = 12;
// Function Call
if (isLynchBell(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
时间复杂度: O(n)
参考:http://www.numbersaplenty.com/set/Lynch-Bell_number/