给定整数N ,任务是检查N是否为Nude数字。
A Nude number is a number that is divisible by all of its digits (which should be nonzero).
例子:
Input: N = 672
Output: Yes
Explanation:
Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output is Yes.
Input: N = 450
Output: No
Explanation:
Since, 450 is not divisible by 0 (Also it gives exception). Therefore the output is No.
方法:从数字一开始提取数字并检查以下两个条件:
- 该数字必须为非零,以避免出现异常
- 并且数字必须除以数字N
当N的所有数字都满足这两个条件时,该数字称为“裸数”。
下面是上述方法的实现:
C++
// C++ program to check if the
// number if Nude number or not
#include
using namespace std;
// Check if all digits
// of num divide num
bool checkDivisbility(int num)
{
// array to store all digits
// of the given number
int digit;
int N = num;
while (num != 0) {
digit = num % 10;
num = num / 10;
// If any of the condition
// is true for any digit
// Then N is not a nude number
if (digit == 0 || N % digit != 0)
return false;
}
return true;
}
// Driver code
int main()
{
int N = 128;
bool result = checkDivisbility(N);
if (result)
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to check if the
// number if Nude number or not
import java.util.*;
class GFG{
// Check if all digits
// of num divide num
static boolean checkDivisbility(int num)
{
// array to store all digits
// of the given number
int digit;
int N = num;
while (num != 0)
{
digit = num % 10;
num = num / 10;
// If any of the condition
// is true for any digit
// Then N is not a nude number
if (digit == 0 || N % digit != 0)
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
int N = 128;
boolean result = checkDivisbility(N);
if (result)
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to check if the
# number if nude number or not
# Check if all digits
# of num divide num
def checkDivisbility(num):
# Array to store all digits
# of the given number
digit = 0
N = num
while (num != 0):
digit = num % 10
num = num // 10
# If any of the condition
# is true for any digit
# then N is not a nude number
if (digit == 0 or N % digit != 0):
return False
return True
# Driver code
if __name__ == '__main__':
N = 128
result = checkDivisbility(N)
if (result):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program to check if the
// number if Nude number or not
using System;
class GFG{
// Check if all digits
// of num divide num
static bool checkDivisbility(int num)
{
// array to store all digits
// of the given number
int digit;
int N = num;
while (num != 0)
{
digit = num % 10;
num = num / 10;
// If any of the condition
// is true for any digit
// Then N is not a nude number
if (digit == 0 || N % digit != 0)
return false;
}
return true;
}
// Driver code
public static void Main()
{
int N = 128;
bool result = checkDivisbility(N);
if (result)
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Nidhi_biet
Javascript
输出:
Yes
时间复杂度: O(N的长度)