原文:

给定两个数组ab,从数组a中选择一个元素,数组b中另一个元素,从中选取一个随机对。输出该对被最大加权的概率。

示例

input : a[] = 1 2 3
        b[] = 1 3 3
output : 0.222
explanation : possible pairs are : {1, 1}, 
{1, 3}, {1, 3}, {2, 1}, {2, 3}, {2, 3},
{3, 1}, {3, 3}, {3, 3} i.e. 9.
the pair with maximum weight is {3, 3} with
frequency 2\. so, the probability of random 
pair being maximum is 2/9 = 0.2222.

暴力法:以n ^ 2的时间复杂度生成所有可能的对,并计算最大加权对。

更好的方法:对两个数组都进行排序,并对ab中的最后一个(最大)元素进行计数。最大加权对的数量将是(count1 * count2) / sizeof(a) * sizeof(b)

最佳方法:最佳方法将是遍历两个数组并计算最大元素。 最大加权对的数量将是两个计数的乘积。 概率将是(count1 * count2) / sizeof(a) * sizeof(b)

下面是实现:

c

#include  
using namespace std; 
// function to return probability 
double probability(int a[], int b[], int size1,  
                                     int size2) 
{ 
    // count occurrences of maximum element  
    // in a[] 
    int max1 = int_min,  count1 = 0; 
    for (int i = 0; i < size1; i  ) { 
        if (a[i] > max1) { 
            max1 = a[i]; 
            count1 = 1; 
        } 
        else if (a[i] == max1) { 
            count1  ; 
        } 
    } 
    // count occurrences of maximum element  
    // in b[] 
    int max2 = int_min, count2 = 0; 
    for (int i = 0; i < size2; i  ) { 
        if (b[i] > max2) { 
            max2 = b[i]; 
            count2 = 1; 
        } 
        else if (b[i] == max2) { 
            count2  ; 
        } 
    } 
    // returning probability 
    return (double)(count1 * count2) /  
                  (size1 * size2); 
} 
// driver code 
int main() 
{ 
    int a[] = { 1, 2, 3 }; 
    int b[] = { 1, 3, 3 }; 
    int size1 = sizeof(a) / sizeof(a[0]); 
    int size2 = sizeof(b) / sizeof(b[0]); 
    cout << probability(a, b, size1, size2); 
    return 0; 
} 

java

// java program to find probability  
// of a random pair being the maximum 
// weighted pair 
import java.io.*; 
  
class gfg { 
      
    // function to return probability 
    static double probability(int a[], int b[],  
                            int size1,int size2) 
    { 
        // count occurrences of maximum  
        // element in a[] 
        int max1 = integer.min_value,  count1 = 0; 
        for (int i = 0; i < size1; i  ) { 
            if (a[i] > max1) { 
                max1 = a[i]; 
                count1 = 1; 
            } 
            else if (a[i] == max1) { 
                count1  ; 
            } 
        } 
       
        // count occurrences of maximum  
        // element in b[] 
        int max2 = integer.min_value, count2 = 0; 
        for (int i = 0; i < size2; i  ) { 
            if (b[i] > max2) { 
                max2 = b[i]; 
                count2 = 1; 
            } 
            else if (b[i] == max2) { 
                count2  ; 
            } 
        } 
       
        // returning probability 
        return (double)(count1 * count2) / (size1 * size2); 
    } 
       
    // driver code 
    public static void main(string args[]) 
    { 
        int a[] = { 1, 2, 3 }; 
        int b[] = { 1, 3, 3 }; 
       
        int size1 = a.length; 
        int size2 = b.length; 
       
        system.out.println(probability(a, b,  
                            size1, size2)); 
    } 
} 
  
/*this code is contributed by nikita tiwari.*/

python3

import sys 
  
# function to return probability  
def probability(a, b, size1, size2): 
  
    # count occurrences of maximum 
    # element in a[]  
    max1 = -(sys.maxsize - 1) 
    count1 = 0
    for i in range(size1): 
        if a[i] > max1: 
            count1 = 1
        elif a[i] == max1: 
            count1  = 1
  
    # count occurrences of maximum  
    # element in b[]  
    max2 = -(sys.maxsize - 1) 
    count2 = 0
    for i in range(size2): 
        if b[i] > max2: 
            max2 = b[i] 
            count2 = 1
        elif b[i] == max2: 
            count2  = 1
  
    # returning probability  
    return round((count1 * count2) / 
                 (size1 * size2), 6) 
  
# driver code 
a = [1, 2, 3] 
b = [1, 3, 3] 
size1 = len(a) 
size2 = len(b) 
print(probability(a, b, size1, size2)) 
  
# this code is contributed  
# by shrikant13

c

// c# program to find probability of a random  
// pair being the maximum weighted pair 
using system; 
  
class gfg { 
      
    // function to return probability 
    static float probability(int []a, int []b,  
                          int size1,int size2) 
    { 
          
        // count occurrences of maximum  
        // element in a[] 
        int max1 = int.minvalue, count1 = 0; 
          
        for (int i = 0; i < size1; i  ) { 
            if (a[i] > max1) { 
                max1 = a[i]; 
                count1 = 1; 
            } 
            else if (a[i] == max1) { 
                count1  ; 
            } 
        } 
      
        // count occurrences of maximum  
        // element in b[] 
        int max2 = int.minvalue, count2 = 0; 
          
        for (int i = 0; i < size2; i  ) { 
            if (b[i] > max2) { 
                max2 = b[i]; 
                count2 = 1; 
            } 
            else if (b[i] == max2) { 
                count2  ; 
            } 
        } 
      
        // returning probability 
        return (float)(count1 * count2) /  
                            (size1 * size2); 
    } 
      
    // driver code 
    public static void main() 
    { 
        int []a = { 1, 2, 3 }; 
        int []b = { 1, 3, 3 }; 
      
        int size1 = a.length; 
        int size2 = b.length; 
      
        console.writeline(probability(a, b,  
                            size1, size2)); 
    } 
} 
  
/* this code is contributed by vt_m.*/

php

 $max1) 
        { 
            $max1 = $a[$i]; 
            $count1 = 1; 
        } 
        else if ($a[$i] == $max1) 
        { 
            $count1  ; 
        } 
    } 
  
    // count occurrences of maximum  
    // element in b[] 
    $max2 = php_int_min; $count2 = 0; 
    for ($i = 0; $i < $size2; $i  )  
    { 
        if ($b[$i] > $max2)  
        { 
            $max2 = $b[$i]; 
            $count2 = 1; 
        } 
        else if ($b[$i] == $max2)  
        { 
            $count2  ; 
        } 
    } 
  
    // returning probability 
    return (double)($count1 * $count2) /  
                     ($size1 * $size2); 
} 
  
    // driver code 
    $a = array(1, 2, 3); 
    $b = array(1, 3, 3); 
    $size1 = sizeof($a); 
    $size2 = sizeof($b); 
    echo probability($a, $b,  
            $size1, $size2); 
      
// this code is contributed by ajit 
?>

输出:

0.222222