📜  两列乘积的最大和

📅  最后修改于: 2021-04-23 17:45:04             🧑  作者: Mango

给定两个具有相同大小N的正整数的数组A和B。任务是找到其元素乘积的最大和。 A中的每个元素必须与B中的一个元素正好相乘,反之亦然,以使两个数组中的每个元素正好出现一次,并且获得的乘积之和最大。
例子

这样做的目的是观察到两个最大数的乘积将对乘积的最大和作出贡献。因此,想法是:

  1. 对两个数组进行排序。
  2. 遍历数组,并计算具有相同索引的数组元素的乘积之和。

下面是上述方法的实现:

C++
// CPP program to calculate maximum sum
// of products of two arrays
#include
using namespace std;
 
    // Function that calculates maximum sum
    // of products of two arrays
    int maximumSOP(int *a, int *b)
    {
        // Variable to store the sum of
        // products of array elements
        int sop = 0;
 
        // length of the arrays
        int n = sizeof(a)/sizeof(a[0]);
 
        // Sorting both the arrays
        sort(a,a+n+1);
        sort(b,b+n+1);
 
        // Traversing both the arrays
        // and calculating sum of product
        for (int i = 0; i <=n; i++) {
            sop += a[i] * b[i];
        }
 
        return sop;
    }
 
    // Driver code
    int main()
    {
        int A[] = { 1, 2, 3 };
        int B[] = { 4, 5, 1 };
 
        cout<


Java
// Java program to calculate maximum sum
// of products of two arrays
 
import java.io.*;
import java.util.*;
public class GFG {
 
    // Function that calculates maximum sum
    // of products of two arrays
    static int maximumSOP(int[] a, int[] b)
    {
        // Variable to store the sum of
        // products of array elements
        int sop = 0;
 
        // length of the arrays
        int n = a.length;
 
        // Sorting both the arrays
        Arrays.sort(a);
        Arrays.sort(b);
 
        // Traversing both the arrays
        // and calculating sum of product
        for (int i = 0; i < n; i++) {
            sop += a[i] * b[i];
        }
 
        return sop;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int[] A = { 1, 2, 3 };
        int[] B = { 4, 5, 1 };
 
        System.out.println(maximumSOP(A, B));
    }
}


Python 3
# Python program to calculate
# maximum sum of products of
# two arrays
 
# Function that calculates
# maximum sum of products
# of two arrays
def maximumSOP(a, b) :
 
    # Variable to store the sum of
    # products of array elements
    sop = 0
 
    # length of the arrays
    n = len(a)
 
    # Sorting both the arrays
    a.sort()
    b.sort()
 
    # Traversing both the arrays
    # and calculating sum of product
    for i in range(n) :
        sop += a[i] * b[i]
 
    return sop
 
# Driver code    
if __name__ == "__main__" :
 
    A = [1, 2, 3]
    B = [4, 5, 1]
 
    print(maximumSOP(A, B))
 
# This code is contributed by ANKITRAI1
C# // C# program to calculate maximum sum
// of products of two arrays
using System;

class GFG 
{

// Function that calculates maximum 
// sum of products of two arrays
static int maximumSOP(int[] a, int[] b)
{
    // Variable to store the sum of
    // products of array elements
    int sop = 0;

    // length of the arrays
    int n = a.Length;

    // Sorting both the arrays
    Array.Sort(a);
    Array.Sort(b);

    // Traversing both the arrays
    // and calculating sum of product
    for (int i = 0; i < n; i++) 
    {
        sop += a[i] * b[i];
    }

    return sop;
}

// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3 };
    int[] B = { 4, 5, 1 };

    Console.Write(maximumSOP(A, B));
}
}

// This code is contributed 
// by ChitraNayal


PHP


Javascript


输出:
24