📌  相关文章
📜  检查给定数组是否包含某个整数的所有除数

📅  最后修改于: 2021-04-29 09:06:11             🧑  作者: Mango

给定一个整数数组arr [] ,任务是检查该数组是否包含某个整数的所有除数。

例子:

方法:如果数组包含特定整数的所有除数,例如X,则数组arr []中的最大元素为整数X。现在,找到数组arr []的最大元素,并计算其所有除数,并将其存储在向量b中。如果数组arr []与向量b相等,则该数组包含特定整数的所有除数,否则为否。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if arr[]
// contains all the divisors of some integer
bool checkDivisors(int a[], int n)
{
  
    // Maximum element from the array
    int X = *max_element(a, a + n);
  
    // Vector to store divisors
    // of the maximum element i.e. X
    vector b;
  
    // Store all the divisors of X
    for (int i = 1; i * i <= X; i++) {
        if (X % i == 0) {
            b.push_back(i);
            if (X / i != i)
                b.push_back(X / i);
        }
    }
  
    // If the lengths of a[]
    // and b are different
    // return false
    if (b.size() != n)
        return false;
  
    // Sort a[] and b
    sort(a, a + n);
    sort(b.begin(), b.end());
  
    for (int i = 0; i < n; i++) {
  
        // If divisors are not
        // equal return false
        if (b[i] != a[i])
            return false;
    }
  
    return true;
}
  
// Driver code
int main()
{
    int arr[] = { 8, 1, 2, 12, 48,
                  6, 4, 24, 16, 3 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    if (checkDivisors(arr, N))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// returns th maximum element of the array
static int max_element(int a[] )
{
    int m = a[0];
    for(int i = 0; i < a.length; i++)
    m = Math.max(a[i], m);
    return m;
}
  
// Function that returns true if arr[]
// contains all the divisors of some integer
static boolean checkDivisors(int a[], int n)
{
  
    // Maximum element from the array
    int X = max_element(a);
  
    // Vector to store divisors
    // of the maximum element i.e. X
    Vector b=new Vector();
  
    // Store all the divisors of X
    for (int i = 1; i * i <= X; i++) 
    {
        if (X % i == 0) 
        {
            b.add(i);
            if (X / i != i)
                b.add(X / i);
        }
    }
  
    // If the lengths of a[]
    // and b are different
    // return false
    if (b.size() != n)
        return false;
  
    // Sort a[] and b
    Arrays.sort(a);
    Collections.sort(b);
  
    for (int i = 0; i < n; i++)
    {
  
        // If divisors are not
        // equal return false
        if (b.get(i) != a[i])
            return false;
    }
  
    return true;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 8, 1, 2, 12, 48,
                6, 4, 24, 16, 3 };
  
    int N = arr.length;
  
    if (checkDivisors(arr, N))
        System.out.println("Yes");
    else
        System.out.println("No");
  
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Python 3 implementation of the approach
from math import sqrt
  
# Function that returns true if arr[]
# contains all the divisors of some integer
def checkDivisors(a,n):
    # Maximum element from the array
    X = max(a)
  
    # Vector to store divisors
    # of the maximum element i.e. X
    b = []
  
    # Store all the divisors of X
    for i in range(1,int(sqrt(X))+1):
        if (X % i == 0):
            b.append(i)
            if (X // i != i):
                b.append(X // i)
  
    # If the lengths of a[]
    # and b are different
    # return false
    if (len(b) != n):
        return False
  
    # Sort a[] and b
    a.sort(reverse = False)
    b.sort(reverse = False)
  
    for i in range(n):
        # If divisors are not
        # equal return false
        if (b[i] != a[i]):
            return False
    return True
  
# Driver code
if __name__ == '__main__':
    arr = [8, 1, 2, 12, 48,6, 4, 24, 16, 3]
  
    N = len(arr)
  
    if (checkDivisors(arr, N)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GFG
{
  
// returns th maximum element of the array
static int max_element(int []a )
{
    int m = a[0];
    for(int i = 0; i < a.Length; i++)
    m = Math.Max(a[i], m);
    return m;
}
  
// Function that returns true if arr[]
// contains all the divisors of some integer
static bool checkDivisors(int []a, int n)
{
  
    // Maximum element from the array
    int X = max_element(a);
  
    // Vector to store divisors
    // of the maximum element i.e. X
    List b = new List();
  
    // Store all the divisors of X
    for (int i = 1; i * i <= X; i++) 
    {
        if (X % i == 0) 
        {
            b.Add(i);
            if (X / i != i)
                b.Add(X / i);
        }
    }
  
    // If the lengths of a[]
    // and b are different
    // return false
    if (b.Count != n)
        return false;
  
    // Sort a[] and b
    Array.Sort(a);
    b.Sort();
  
    for (int i = 0; i < n; i++)
    {
  
        // If divisors are not
        // equal return false
        if (b[i] != a[i])
            return false;
    }
  
    return true;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 8, 1, 2, 12, 48,
                6, 4, 24, 16, 3 };
  
    int N = arr.Length;
  
    if (checkDivisors(arr, N))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
  
} 
}
  
// This code is contributed by Princi Singh


输出:
Yes