숫자 'n'의 배열이 주어지고 과제는 무작위로 선택된 3개의 숫자가 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
아래 프로그램에서 사용된 접근 방식은 다음과 같습니다. -
- 양의 정수 배열 입력
- 배열 크기 계산
-
3개의 난수가 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