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
|
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 }
本文解析了一道Uva11181概率题,通过递归搜索和标记数组的方法来计算给定条件下每个人购买商品的概率。详细介绍了如何使用条件概率公式解决此类问题,并附带了完整的C++代码实现。
309

被折叠的 条评论
为什么被折叠?



