N friends go to the local super market together. The probability of their buying something from the
market is p 1 ,p 2 ,p 3 ,...,p N respectively. After their marketing is finished you are given the information
that exactly r of them has bought something and others have bought nothing. Given this information
you will have to find their individual buying probability.
Input
The input file contains at most 50 sets of inputs. The description of each set is given below:
First line of each set contains two integers N (1 ≤ N ≤ 20) and r (0 ≤ r ≤ N). Meaning of N and
r are given in the problem statement. Each of the next N lines contains one floating-point number p i
(0.1 < p i < 1) which actually denotes the buying probability of the i-th friend. All probability values
should have at most two digits after the decimal point.
Input is terminated by a case where the value of N and r is zero. This case should not be processes.
Output
For each line of input produce N +1 lines of output. First line contains the serial of output. Each of the
next N lines contains a floating-point number which denotes the buying probability of the i-th friend
given that exactly r has bought something. These values should have six digits after the decimal point.
Follow the exact format shown in output for sample input. Small precision errors will be allowed. For
reasonable precision level use double precision floating-point numbers.
Sample Input
3 2
0.10
0.20
0.30
5 1
0.10
0.10
0.10
0.10
0.10
0 0
Sample Output
Case 1:
0.413043
0.739130
0.847826
Case 2:
0.200000
0.200000
0.200000
0.200000
0.200000
题意:
有 n 个人准备去逛超市,其中第 i 人购物的概率是 Pi ,逛完以后得知有 r 人买了东西,根据这一信息,计算每人买了东西的实际概率。
x[i] 表示 第i个人是否买了东西 1买了0没买
然后用 next_permutation();
把所有的情况 表示出来
在暴力求解了。。。条件概率,贝叶斯公式
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; double r[21]; double sum[21]; int x[21]; double tot; int n,m; void init(){ memset(r,0,sizeof(r)); memset(sum,0,sizeof(sum)); memset(x,1,sizeof(x)); for(int i=0;i<n-m;i++){ x[i]=0; } tot=0; for(int i=0;i<n;i++){ cin>>r[i]; } } int main(){ int k=1;double temp=0.0; while(cin>>n>>m&&n+m){ init(); do{ temp=1; for(int i=0;i<n;i++){ if(x[i])temp*=r[i]; else temp*=1-r[i]; } tot+=temp; for(int i=0;i<n;i++){ if(x[i])sum[i]+=temp; } }while(next_permutation(x,x+n)); cout<<"Case "<<k++<<":"<<endl; for(int i=0;i<n;i++){ printf("%f\n",sum[i]/tot); } } }