可以在AP中随机选择三个数字的C ++程序概率
给定一个带有数字“n”的数组,任务是找到三个随机选择的数字出现在AP中的概率。
示例
Input-: arr[] = { 2,3,4,7,1,2,3 }
Output-: Probability of three random numbers being in A.P is: 0.107692
Input-:arr[] = { 1, 2, 3, 4, 5 }
Output-: Probability of three random numbers being in A.P is: 0.151515以下程序中使用的方法如下-
输入正整数数组
计算数组的大小
应用下面给出的公式找出三个随机数出现在AP中的概率
3n/(4(n*n)–1)
打印结果
算法
Start
Step 1-> function to calculate the probability of three random numbers be in AP
double probab(int n)
return (3.0 * n) / (4.0 * (n * n) - 1)
Step 2->In main()
declare an array of elements as int arr[] = { 2,3,4,7,1,2,3 }
calculate size of an array as int size = sizeof(arr)/sizeof(arr[0])
call the function to calculate probability as probab(size)
Stop示例
#include <bits/stdc++.h>
using namespace std;
//calculate probability of three random numbers be in AP
double probab(int n) {
return (3.0 * n) / (4.0 * (n * n) - 1);
}
int main() {
int arr[] = { 2,3,4,7,1,2,3 };
int size = sizeof(arr)/sizeof(arr[0]);
cout<<"probability of three random numbers being in A.P is : "<<probab(size);
return 0;
}输出结果
Probability of three random numbers being in A.P is: 0.107692