Uva--11181(数学,全概率,条件概率)

2014-09-03 16:32:21

Problem G
Probability|Given
Input: Standard Input

Output: Standard Output

 

N friends go to the local super market together. The probability of their buying something from the market is 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  (0.1<<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                             Output for Sample Input

3 2
0.10
0.20
0.30
5 1
0.10
0.10
0.10
0.10
0.10
0 0

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000

 

 

 
思路:挺好的小白书例题,概率这种东西一定要把公式推对,差一点都不行。
  “把 r 个人买了东西”叫做事件 E,“第 i 个人买了东西”叫做事件 Ei,那么要求的每个人的概率就是 P(Ei | E),条件概率公式:P(Ei | E) = P(Ei * E) / P(E),求出P(E)和P(Ei * E)即可。开一个标记数组,然后递归搜索每一个人,枚举他买或不买,买的话用标记数组记为 1,扫完所有人且标记数组里的 1 的个数为 r 时,就是 P(E)的一部分,加上。同时在递归判断结束时(即 1的个数为 r 时,再扫一遍标记数组,如果第 i 位为 1,就说明当时的概率是 P(Ei | E) 的一部分)
 1 /*************************************************************************
 2     > File Name: Uva11181.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Wed 03 Sep 2014 03:22:49 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int N,r;
17 int used[25];
18 double p[25],pe;
19 double p2[25];
20 
21 void Cal(int id,int cnt,double pro){
22     if(id > N){
23         if(cnt == r){
24             pe += pro;
25             for(int i = 1; i <= N; ++i) if(used[i])
26                 p2[i] += pro;
27         }
28         return;
29     }
30     if(r - cnt <= N - id)
31         Cal(id + 1,cnt,pro * (1.0 - p[id]));
32     if(cnt < r){
33         used[id] = 1;
34         Cal(id + 1,cnt + 1,pro * p[id]);
35         used[id] = 0;
36     }
37 }
38 
39 int main(){
40     int Case = 0;
41     while(scanf("%d%d",&N,&r) != EOF){
42         if(N == 0 && r == 0) break;
43         for(int i = 1; i <= N; ++i) scanf("%lf",&p[i]);
44         pe = 0.0;
45         memset(used,0,sizeof(used));
46         memset(p2,0,sizeof(p2));
47         Cal(1,0,1.0);
48         printf("Case %d:\n",++Case);
49         for(int i = 1; i <= N; ++i) printf("%.6lf\n",p2[i] / pe);
50     }
51     return 0;
52 }

 

 
posted @ 2014-09-03 16:39  Naturain  阅读(197)  评论(0编辑  收藏  举报