English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C dans le programme où l'on peut choisir trois nombres au hasard dans l'AP ++Probabilité du programme

Given an array with the number "n", the task is to find the probability that three randomly selected numbers appear in 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中的概率

    3 n /(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