ด้วยอาร์เรย์ของตัวเลข '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
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