📌  相关文章
📜  将一个数组的每个元素除以其他数组元素

📅  最后修改于: 2021-04-27 18:18:02             🧑  作者: Mango

给定两个数组,要执行的操作是a []的每个元素都应除以b []的所有元素,并且必须计算其底值。
例子 :

Input : a[] = {5, 100, 8}, b[] = {2, 3}
Output : 0 16 1
Explanation :
Size of a[] is 3.
Size of b[] is 2.
Now 5 has to be divided by the elements of array b[]
i.e. 5 is divided by 2, then the quotient obtained 
is divided by 3 and the floor value of this is 
calculated. The same process is repeated for the other
array elements.

第一种方法:该解决方案的复杂度为O(n * m),其中a []的大小为n,数组b []的大小为m。在此解决方案中,我们修复数组a []的元素,并使用数组b []的元素对其进行迭代。
第二种方法:在这种方法中,我们使用了简单的数学运算。我们首先找到数组B的乘积,然后将其除以a []的每个数组元素
该解决方案的复杂度为O(n)。

C++
// CPP program to find quotient of array
// elements
#include 
using namespace std;
 
// Function to calculate the quotient
// of every element of the array
void calculate(int a[], int b[], int n, int m)
{
    int mul = 1;
 
    // Calculate the product of all elements
    for (int i = 0 ; i < m ; i++)
        if (b[i] != 0)
            mul = mul * b[i];
 
    // To calculate the quotient of every
    // array element
    for (int i = 0 ; i < n ; i++)
    {
        int x = floor(a[i] / mul);
        cout << x << " ";
    }
}
 
// Driver code
int main()
{
    int a[] = {5 , 100 , 8};
    int b[] = {2 , 3};
    int n = sizeof(a)/sizeof(a[0]);
    int m = sizeof(b)/sizeof(b[0]);
    calculate(a, b, n, m);
    return 0;
}


Java
// Java program to find quotient of array
// elements
 
import java.io.*;
 
class GFG {
 
    // Function to calculate the quotient
    // of every element of the array
    static void calculate(int a[], int b[],
                                int n, int m)
    {
         
        int mul = 1;
 
        // Calculate the product of all
        // elements
        for (int i = 0; i < m; i++)
            if (b[i] != 0)
                mul = mul * b[i];
 
        // To calculate the quotient of every
        // array element
        for (int i = 0; i < n; i++) {
            int x = (int)Math.floor(a[i] / mul);
            System.out.print(x + " ");
        }
    }
 
    public static void main(String[] args)
    {
        int a[] = { 5, 100, 8 };
        int b[] = { 2, 3 };
        int n = a.length;
        int m = b.length;
         
        calculate(a, b, n, m);
    }
}
 
// This code is contributed by Ajit.


Python3
# Python3 program to find
# quotient of arrayelements
import math
 
# Function to calculate the quotient
# of every element of the array
def calculate(a, b, n, m):
         
    mul = 1
 
    # Calculate the product
    # of all elements
    for i in range(m):
        if (b[i] != 0):
            mul = mul * b[i]
 
    # To calculate the quotient
    # of every array element
    for i in range(n):
        x = math.floor(a[i] / mul)
        print(x, end = " ")
         
 
# Driver code
a = [ 5, 100, 8 ]
b = [ 2, 3 ]
n = len(a)
m = len(b)
         
calculate(a, b, n, m)
 
# This code is contributed by Anant Agarwal.


C#
// C# program to find quotient
// of array elements
using System;
 
class GFG {
  
    // Function to calculate the quotient
    // of every element of the array
    static void calculate(int []a, int []b,
                              int n, int m)
    {
        int mul = 1;
  
        // Calculate the product of all
        // elements
        for (int i = 0; i < m; i++)
            if (b[i] != 0)
                mul = mul * b[i];
  
        // To calculate the quotient of every
        // array element
        for (int i = 0; i < n; i++) {
            int x = (int)Math.Floor((double)(a[i] / mul));
            Console.Write(x + " ");
        }
    }
     
    // Driver code
    public static void Main()
    {
        int []a = { 5, 100, 8 };
        int []b = { 2, 3 };
        int n = a.Length;
        int m = b.Length;
          
        calculate(a, b, n, m);
    }
}
  
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出 :

0 16 1