📌  相关文章
📜  使数组的所有数字相等

📅  最后修改于: 2021-04-26 08:45:19             🧑  作者: Mango

给定一个数组arr [] ,任务是使所有数组元素与给定操作相等。在单个操作中,数组的任何元素都可以乘以23 。如果有可能使所有数组元素与给定操作相等,则输出Yes,否则输出No。

例子:

方法:任何正整数都可以分解并写为2 a * 3 b * 5 c * 7 d *…..
我们可以将给定数字乘以23,以便为它们增加ab 。因此,我们可以通过将ab增加到相同的大值(例如100)来使它们相等。但是我们无法更改其他质数的幂,因此它们从一开始就必须相等。我们可以通过将输入中的所有数字尽可能多地除以2和3来检查它。那么所有的人必须相等。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if all 
// the array elements can be made equal
// with the given operation
bool EqualNumbers(int a[], int n)
{
    for (int i = 0; i < n; i++) {
  
        // Divide number by 2
        while (a[i] % 2 == 0)
            a[i] /= 2;
  
        // Divide number by 3
        while (a[i] % 3 == 0)
            a[i] /= 3;
  
        if (a[i] != a[0]) {
            return false;
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
    int a[] = { 50, 75, 150 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    if (EqualNumbers(a, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of above approach
class GFG 
{
  
    // Function that returns true if all 
    // the array elements can be made equal 
    // with the given operation 
    static boolean EqualNumbers(int a[], int n)
    {
        for (int i = 0; i < n; i++) 
        {
  
            // Divide number by 2 
            while (a[i] % 2 == 0) 
            {
                a[i] /= 2;
            }
  
            // Divide number by 3 
            while (a[i] % 3 == 0)
            {
                a[i] /= 3;
            }
  
            if (a[i] != a[0])
            {
                return false;
            }
        }
  
        return true;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int a[] = {50, 75, 150};
  
        int n = a.length;
  
        if (EqualNumbers(a, n)) 
        {
            System.out.println("Yes");
        } 
        else 
        {
            System.out.println("No");
        }
    }
} 
  
// This code is contributed by Rajput-JI


Python3
# Python3 implementation of the approach 
  
# Function that returns true if all 
# the array elements can be made equal 
# with the given operation 
def EqualNumbers(a, n): 
  
    for i in range(0, n): 
  
        # Divide number by 2 
        while a[i] % 2 == 0: 
            a[i] //= 2
  
        # Divide number by 3 
        while a[i] % 3 == 0: 
            a[i] //= 3
  
        if a[i] != a[0]: 
            return False
  
    return True
  
# Driver code 
if __name__ == "__main__":
  
    a = [50, 75, 150] 
    n = len(a) 
  
    if EqualNumbers(a, n):
        print("Yes")
    else:
        print("No") 
  
# This code is contributed by Rituraj Jain


C#
// C# implementation of above approach 
using System;
  
class GFG 
{ 
  
    // Function that returns true if all 
    // the array elements can be made equal 
    // with the given operation 
    static bool EqualNumbers(int []a, int n) 
    { 
        for (int i = 0; i < n; i++) 
        { 
  
            // Divide number by 2 
            while (a[i] % 2 == 0) 
            { 
                a[i] /= 2; 
            } 
  
            // Divide number by 3 
            while (a[i] % 3 == 0) 
            { 
                a[i] /= 3; 
            } 
  
            if (a[i] != a[0]) 
            { 
                return false; 
            } 
  
        } 
  
        return true; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int []a = {50, 75, 150}; 
  
        int n = a.Length; 
  
        if (EqualNumbers(a, n)) 
        { 
            Console.WriteLine("Yes"); 
        } 
        else
        { 
            Console.WriteLine("No"); 
        } 
    } 
} 
  
// This code is contributed by Ryuga


PHP


输出:
Yes