Solution : Probability|Given
This problem is related to conditional probability.
This problem given n people and the probability of each person will shop and also given r that is r person will shop and the rest will not shop.
# P(B) = Probability of B.
Conditional Probability : Suppose A and B are two event. A event depend on B event and A event occur after the B event. We can write this P(A | B) and its means A event occur after the B event occur.
Now according to Conditional Probability we can write this,
P(A | B) = P(A ∩ B) / P(B)
= n(A ∩ B) / n(B)
update comming.....................
#include<bits/stdc++.h>
using namespace std;
double prob[50], total[50], n, r;
double Count(int pos, int k, double p)
{
if(pos == n)
{
if(k == r)
return p;
else
return 0;
}
double r1 = 0, r2 = 0;
r1 = Count( pos+1, k, p*(1 - prob[pos]) );
if(k < r)
{
r2 = Count( pos+1, k+1, p*prob[pos] );
total[pos] += r2;
}
return r1+r2;
}
int main()
{
int j = 1;
while(cin >> n >> r)
{
if(n == 0 && r == 0)
break;
for(int i=0; i<n; i++)
{
cin >> prob[i];
total[i] = 0;
}
double total_probability = Count(0, 0, 1);
cout << showpoint;
cout << fixed;
cout << setprecision(6);
cout << "Case " << j++ << ":\n";
for(int i=0; i<n; i++)
cout << total[i]/total_probability << "\n";
}
return 0;
}
Comments
Post a Comment