给定M个唯一数字和一个数字N。任务是查找可以由给定M个数字形成的N个数字的数目,该数字可以被5整除,并且没有重复的数字。
注意:如果无法从给定的数字中形成N位数字,请打印-1。
例子:
Input : N = 3, M = 6, digits[] = {2, 3, 5, 6, 7, 9}
Output : 20
Input : N = 5, M = 6, digits[] = {0, 3, 5, 6, 7, 9}
Output : 240
对于要被5整除的数字,唯一的条件是数字中单位位置的数字必须为0或5 。
因此,要查找可以被给定数字形成的可被5整除的数字计数,请执行以下操作:
- 检查给定的数字是否同时包含0和5。
- 如果给定的数字同时包含0和5,则可以用2种方式填充单位位置,否则可以用1种方式填充单位位置。
- 现在,十位数可以用剩余的M-1个数字填充。因此,有(M-1)种填充十位的方法。
- 同样,现在可以用其余(M-2)个数字中的任意一个填充百位,依此类推。
因此,如果给定的数字同时具有0和5:
Required number of numbers = 2 * (M-1)* (M-2)...N-times.
否则,如果给定的数字具有0和5之一而不是两者都为:
Required number of numbers = 1 * (M-1)* (M-2)...N-times.
下面是上述方法的实现。
C++
// CPP program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
#include
using namespace std;
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
int numbers(int n, int arr[], int m)
{
int isZero = 0, isFive = 0;
int result = 0;
// If it is not possible to form
// n digit number from the given
// m digits without repetition
if (m < n) {
return -1;
}
for (int i = 0; i < m; i++) {
if (arr[i] == 0)
isZero = 1;
if (arr[i] == 5)
isFive = 1;
}
// If both zero and five exists
if (isZero && isFive) {
result = 2;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
}
else if (isZero || isFive) {
result = 1;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
}
else
result = -1;
return result;
}
// Driver code
int main()
{
int n = 3, m = 6;
int arr[] = { 2, 3, 5, 6, 7, 9 };
cout << numbers(n, arr, m);
return 0;
}
Java
// Java program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
class GFG {
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
static int numbers(int n, int arr[], int m) {
int isZero = 0, isFive = 0;
int result = 0;
// If it is not possible to form
// n digit number from the given
// m digits without repetition
if (m < n) {
return -1;
}
for (int i = 0; i < m; i++) {
if (arr[i] == 0) {
isZero = 1;
}
if (arr[i] == 5) {
isFive = 1;
}
}
// If both zero and five exists
if (isZero == 1 && isFive == 1) {
result = 2;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else if (isZero == 1 || isFive == 1) {
result = 1;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else {
result = -1;
}
return result;
}
// Driver code
public static void main(String[] args) {
int n = 3, m = 6;
int arr[] = {2, 3, 5, 6, 7, 9};
System.out.println(numbers(n, arr, m));
}
}
// This code is contributed by RAJPUT-JI
Python 3
# Python 3 program to find the count
# of all possible N digit numbers which
# are divisible by 5 formed from M digits
# Function to find the count of all
# possible N digit numbers which are
# divisible by 5 formed from M digits
def numbers(n, arr, m):
isZero = 0
isFive = 0
result = 0
# If it is not possible to form
# n digit number from the given
# m digits without repetition
if (m < n) :
return -1
for i in range(m) :
if (arr[i] == 0):
isZero = 1
if (arr[i] == 5):
isFive = 1
# If both zero and five exists
if (isZero and isFive) :
result = 2
# Remaining N-1 iterations
for i in range( n - 1):
m -= 1
result = result * (m)
elif (isZero or isFive) :
result = 1
# Remaining N-1 iterations
for i in range(n - 1) :
m -= 1
result = result * (m)
else:
result = -1
return result
# Driver code
if __name__ == "__main__":
n = 3
m = 6
arr = [ 2, 3, 5, 6, 7, 9]
print(numbers(n, arr, m))
# This code is contributed by ChitraNayal
C#
// C# program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
using System;
public class GFG {
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
static int numbers(int n, int []arr, int m) {
int isZero = 0, isFive = 0;
int result = 0;
// If it is not possible to form
// n digit number from the given
// m digits without repetition
if (m < n) {
return -1;
}
for (int i = 0; i < m; i++) {
if (arr[i] == 0) {
isZero = 1;
}
if (arr[i] == 5) {
isFive = 1;
}
}
// If both zero and five exists
if (isZero == 1 && isFive == 1) {
result = 2;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else if (isZero == 1 || isFive == 1) {
result = 1;
// Remaining N-1 iterations
for (int i = 0; i < n - 1; i++) {
result = result * (--m);
}
} else {
result = -1;
}
return result;
}
// Driver code
public static void Main() {
int n = 3, m = 6;
int []arr = {2, 3, 5, 6, 7, 9};
Console.WriteLine(numbers(n, arr, m));
}
}
// This code is contributed by RAJPUT-JI
PHP
Javascript
输出:
20