📜  每对从gcd()查找原始数字

📅  最后修改于: 2021-04-23 07:34:57             🧑  作者: Mango

给定一个数组arr [],其中包含另一个数组的每个可能的元素对的GCD。任务是找到用于计算GCD阵列的原始编号。例如,如果原始数字为{4,6,8},则给定的数组将为{4,2,4,2,6,2,2,4,2,8}。

例子:

方法:

  1. 以降序对数组进行排序。
  2. 最高元素将始终是原始数字之一。保留该编号并将其从阵列中删除。
  3. 计算上一步中元素的GCD,使当前元素从最大值开始计算,并丢弃给定数组中的GCD值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Function to find the required numbers
void findNumbers(int arr[], int n)
{
  
    // Sort array in decreasing order
    sort(arr, arr + n, greater());
  
    int freq[arr[0] + 1] = { 0 };
  
    // Count frequency of each element
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;
  
    // Size of the resultant array
    int size = sqrt(n);
    int brr[size] = { 0 }, x, l = 0;
  
    for (int i = 0; i < n; i++) {
        if (freq[arr[i]] > 0) {
  
            // Store the highest element in
            // the resultant array
            brr[l] = arr[i];
  
            // Decrement the frequency of that element
            freq[brr[l]]--;
            l++;
            for (int j = 0; j < l; j++) {
                if (i != j) {
  
                    // Compute GCD
                    x = __gcd(arr[i], brr[j]);
  
                    // Decrement GCD value by 2
                    freq[x] -= 2;
                }
            }
        }
    }
  
    printArr(brr, size);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                  1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    findNumbers(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
  
class GFG
{
  
    // Utility function to print
    // the contents of an array
    static void printArr(int arr[], int n) 
    {
        for (int i = 0; i < n; i++) 
        {
            System.out.print(arr[i] + " ");
        }
    }
  
    // Function to find the required numbers
    static void findNumbers(int arr[], int n) 
    {
  
        // Sort array in decreasing order
        Arrays.sort(arr);
        reverse(arr);
        int freq[] = new int[arr[0] + 1];
  
        // Count frequency of each element
        for (int i = 0; i < n; i++)
        {
            freq[arr[i]]++;
        }
  
        // Size of the resultant array
        int size = (int) Math.sqrt(n);
        int brr[] = new int[size], x, l = 0;
  
        for (int i = 0; i < n; i++)
        {
            if (freq[arr[i]] > 0 && l < size) 
            {
  
                // Store the highest element in
                // the resultant array
                brr[l] = arr[i];
  
                // Decrement the frequency of that element
                freq[brr[l]]--;
                l++;
                for (int j = 0; j < l; j++) 
                {
                    if (i != j) 
                    {
  
                        // Compute GCD
                        x = __gcd(arr[i], brr[j]);
  
                        // Decrement GCD value by 2
                        freq[x] -= 2;
                    }
                }
            }
        }
  
        printArr(brr, size);
    }
      
    // reverse array
    public static void reverse(int[] input) 
    {
        int last = input.length - 1;
        int middle = input.length / 2;
        for (int i = 0; i <= middle; i++) 
        {
            int temp = input[i];
            input[i] = input[last - i];
            input[last - i] = temp;
        }
    }
  
    static int __gcd(int a, int b) 
    {
        if (b == 0) 
        {
            return a;
        }
        return __gcd(b, a % b);
  
    }
      
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2};
        int n = arr.length;
        findNumbers(arr, n);
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
from math import sqrt, gcd
  
# Utility function to print
# the contents of an array
def printArr(arr, n):
    for i in range(n):
        print(arr[i], end = " ")
  
# Function to find the required numbers
def findNumbers(arr, n):
      
    # Sort array in decreasing order
    arr.sort(reverse = True)
  
    freq = [0 for i in range(arr[0] + 1)]
  
    # Count frequency of each element
    for i in range(n):
        freq[arr[i]] += 1
  
    # Size of the resultant array
    size = int(sqrt(n))
    brr = [0 for i in range(len(arr))] 
    l = 0
  
    for i in range(n):
        if (freq[arr[i]] > 0):
              
            # Store the highest element in
            # the resultant array
            brr[l] = arr[i]
  
            # Decrement the frequency of that element
            freq[brr[l]] -= 1
            l += 1
            for j in range(l):
                if (i != j):
                      
                    # Compute GCD
                    x = gcd(arr[i], brr[j])
  
                    # Decrement GCD value by 2
                    freq[x] -= 2
  
    printArr(brr, size)
  
# Driver code
if __name__ == '__main__':
    arr = [1, 1, 1, 1, 1, 1, 1, 1, 
           1, 1, 1, 1, 1, 1, 1, 1,
           1, 5, 5, 5, 7, 10, 12, 2, 2]
    n = len(arr)
    findNumbers(arr, n)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation for above approach
using System;
      
class GFG
{
  
    // Utility function to print
    // the contents of an array
    static void printArr(int []arr, int n) 
    {
        for (int i = 0; i < n; i++) 
        {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Function to find the required numbers
    static void findNumbers(int []arr, int n) 
    {
  
        // Sort array in decreasing order
        Array.Sort(arr);
        reverse(arr);
        int []freq = new int[arr[0] + 1];
  
        // Count frequency of each element
        for (int i = 0; i < n; i++)
        {
            freq[arr[i]]++;
        }
  
        // Size of the resultant array
        int size = (int) Math.Sqrt(n);
        int []brr = new int[size];int x, l = 0;
  
        for (int i = 0; i < n; i++)
        {
            if (freq[arr[i]] > 0 && l < size) 
            {
  
                // Store the highest element in
                // the resultant array
                brr[l] = arr[i];
  
                // Decrement the frequency of that element
                freq[brr[l]]--;
                l++;
                for (int j = 0; j < l; j++) 
                {
                    if (i != j) 
                    {
  
                        // Compute GCD
                        x = __gcd(arr[i], brr[j]);
  
                        // Decrement GCD value by 2
                        freq[x] -= 2;
                    }
                }
            }
        }
  
        printArr(brr, size);
    }
      
    // reverse array
    public static void reverse(int []input) 
    {
        int last = input.Length - 1;
        int middle = input.Length / 2;
        for (int i = 0; i <= middle; i++) 
        {
            int temp = input[i];
            input[i] = input[last - i];
            input[last - i] = temp;
        }
    }
  
    static int __gcd(int a, int b) 
    {
        if (b == 0) 
        {
            return a;
        }
        return __gcd(b, a % b);
  
    }
      
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2};
        int n = arr.Length;
        findNumbers(arr, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP
 0) 
        { 
  
            // Store the highest element in 
            // the resultant array 
            $brr[$l] = $arr[$i]; 
  
            // Decrement the frequency of that element 
            $freq[$brr[$l]]--; 
            $l++; 
            for ($j = 0; $j < $l; $j++) 
            { 
                if ($i != $j) 
                { 
  
                    // Compute GCD 
                    $x = gcd($arr[$i], $brr[$j]); 
                      
                    // Decrement GCD value by 2 
                    $freq[$x] -= 2; 
                } 
            } 
        } 
    } 
  
    printArr($brr, $size); 
} 
  
// Driver code 
$arr = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
             1, 1, 1, 1, 5, 5, 5, 7, 10, 12, 2, 2 ); 
$n = count($arr) ;
findNumbers($arr, $n); 
  
// This code is contributed by Ryuga
?>


输出:
12 10 7 5 1