📌  相关文章
📜  用其他所有元素的乘积替换数组中的每个元素

📅  最后修改于: 2021-05-07 06:40:12             🧑  作者: Mango

给定一个整数数组。将每个元素替换为数组所有其他元素的乘积。

例子:

Input : arr[] = { 2, 3, 3, 5, 7 } 
Output : 315 210 210 126 90

方法 :

  • 首先,取数组所有元素的乘积。
  • 现在,将每个元素替换为乘以该元素的乘积。
  • 打印修改后的数组。

下面是上述方法的实现:

C++
// C++ program to Replace every element
// by the product of all other elements
#include "iostream"
using namespace std;
  
void ReplaceElements(int arr[], int n)
{
    int prod = 1;
  
    // Calculate the product of all the elements
    for (int i = 0; i < n; ++i) {
        prod *= arr[i];
    }
  
    // Replace every element product
    // of all other elements
    for (int i = 0; i < n; ++i) {
        arr[i] = prod / arr[i];
    }
}
  
int main()
{
    int arr[] = { 2, 3, 3, 5, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    ReplaceElements(arr, n);
  
    // Print the modified array.
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    return 0;
}


Java
// Java program to Replace every element
// by the product of all other elements
  
class GFG{
static void ReplaceElements(int arr[], int n)
{
    int prod = 1;
  
    // Calculate the product of all the elements
    for (int i = 0; i < n; ++i) {
        prod *= arr[i];
    }
  
    // Replace every element product
    // of all other elements
    for (int i = 0; i < n; ++i) {
        arr[i] = prod / arr[i];
    }
}
  
public static void main(String[] args)
{
    int arr[] = { 2, 3, 3, 5, 7 };
    int n = arr.length;
  
    ReplaceElements(arr, n);
  
    // Print the modified array.
    for (int i = 0; i < n; ++i) {
        System.out.print(arr[i]+" ");
    }
    System.out.println("");
  
}
}
// This code is contributed by mits


Python 3
# Python 3 program to Replace every 
# element by the product of all 
# other elements
  
def ReplaceElements(arr, n):
    prod = 1
  
    # Calculate the product of
    # all the elements
    for i in range(n):
        prod *= arr[i]
  
    # Replace every element product
    # of all other elements
    for i in range(n) :
        arr[i] = prod // arr[i]
  
# Driver Code
if __name__ == "__main__":
    arr = [ 2, 3, 3, 5, 7 ]
    n = len(arr)
  
    ReplaceElements(arr, n)
  
    # Print the modified array.
    for i in range( n):
        print(arr[i], end = " ")
  
# This code is contributed
# by ChitraNayal


C#
// C# program to Replace every element 
// by the product of all other elements 
using System;
  
class GFG
{
static void ReplaceElements(int []arr, int n) 
{ 
    int prod = 1; 
  
    // Calculate the product of 
    // all the elements 
    for (int i = 0; i < n; ++i) 
    { 
        prod *= arr[i]; 
    } 
  
    // Replace every element product 
    // of all other elements 
    for (int i = 0; i < n; ++i) 
    { 
        arr[i] = prod / arr[i]; 
    } 
} 
  
// Driver Code
static public void Main ()
{
    int []arr = { 2, 3, 3, 5, 7 }; 
    int n = arr.Length; 
      
    ReplaceElements(arr, n); 
      
    // Print the modified array. 
    for (int i = 0; i < n; ++i)
    { 
        Console.Write(arr[i]+" "); 
    } 
    Console.WriteLine(""); 
} 
} 
  
// This code is contributed by ajit


PHP


输出 :

315 210 210 126 90

时间复杂度– O(N)