Được đưa ra với hai mảng khác nhau và nhiệm vụ là tìm xác suất của cặp ngẫu nhiên được chọn là cặp có trọng số lớn nhất.
Một Cặp sẽ chứa một phần tử từ giả sử array1 và một phần tử khác tạo thành một mảng khác, giả sử array2. Vì vậy chương trình phải tìm xác suất để cặp chứa phần tử đầu tiên là phần tử lớn nhất của mảng 1 và phần tử thứ hai là phần tử lớn nhất của mảng 2 để tạo thành cặp có trọng số lớn nhất.
Đầu vào
arr1[] = { 2, 23 }
arr2[] = { 10, 3, 8 } Đầu ra
probability of maximum pair : 0.166667
Giải thích
set of pairs from both the arrays are -: {(2, 10), (2, 3), (2, 8), (23, 10), (23, 3), (23, 8)}
Maximum weighted pair from the given set is: (23, 8)
Probability is : 1 / 6 = 0.166667 Đầu vào -
arr1[] = { 4, 5, 6 }
arr2[] = { 6, 2, 6 } Đầu ra -
probability of maximum pair : 0.222222
Giải thích
set of pairs from both the arrays are -: {(4, 6), (4, 2), (4, 6), (5, 6), (5, 2), (5, 6), (6, 6), (6, 2), (6, 6)}
Maximum weighted pair from the given set is (6, 6) which is occurring twice.
Probability is : 2 / 9 = 0.2222 Phương pháp tiếp cận
-
Nhập các phần tử mảng
-
Tìm các phần tử lớn nhất từ cả hai mảng và tính tổng số các cặp có trọng số lớn nhất được tạo thành
-
Tính xác suất bằng cách chia tổng số cặp có trọng số lớn nhất cho tổng số cặp trong tập
-
In xác suất được tính toán
Thuật toán
Start
Step1→ declare function to calculate maximum weighted pair
double max_pair(int arr1[], int arr2[], int size_1, int size_2)
declare int max_pair1 = INT_MIN, count_1 = 0
Loop For int i = 0 and i < size_1 and i++
IF arr1[i] > max_pair1
Set max_pair1 = arr1[i]
Set count_1 = 1
End
Else IF arr1[i] = max_pair1
Set count_1++
End
End
Declare int max_pair2 = INT_MIN, count_2 = 0
Loop For int i = 0 and i < size_2 and i++
IF arr2[i] > max_pair2
Set max_pair2 = arr2[i]
Set count_2 = 1
End
Else IFarr2[i] = max_pair2
Set count_2++
End
End
return (double)(count_1 * count_2) / (size_1 * size_2)
Step 2→ In main()
Declare int arr1[] = { 2, 23 }
Declare int arr2[] = { 10, 3, 8 }
Calculate int size_1 = sizeof(arr1) / sizeof(arr1[0])
Calculate int size_2 = sizeof(arr2) / sizeof(arr2[0])
Call max_pair(arr1, arr2, size_1, size_2)
Stop Ví dụ
#include <bits/stdc++.h>
using namespace std;
// Function to return probability
double max_pair(int arr1[], int arr2[], int size_1, int size_2){
//pair from array 1
int max_pair1 = INT_MIN, count_1 = 0;
for (int i = 0; i < size_1; i++){
if (arr1[i] > max_pair1){
max_pair1 = arr1[i];
count_1 = 1;
}
else if (arr1[i] == max_pair1){
count_1++;
}
}
//pair from array 2
int max_pair2 = INT_MIN, count_2 = 0;
for (int i = 0; i < size_2; i++){
if (arr2[i] > max_pair2){
max_pair2 = arr2[i];
count_2 = 1;
}
else if (arr2[i] == max_pair2){
count_2++;
}
}
return (double)(count_1 * count_2) / (size_1 * size_2);
}
int main(){
int arr1[] = { 2, 23 };
int arr2[] = { 10, 3, 8 };
int size_1 = sizeof(arr1) / sizeof(arr1[0]);
int size_2 = sizeof(arr2) / sizeof(arr2[0]);
cout <<"probability of maximum pair in both the arrays are "<<max_pair(arr1, arr2, size_1, size_2);
return 0;
} Đầu ra
Nếu chạy đoạn mã trên, nó sẽ tạo ra kết quả sau -
probability of maximum pair in both the arrays are 0.166667