uva11181 - Probability|Given(条件概率)
看一位大神的思路,http://blog.csdn.net/wiking__acm/article/details/8636484
条件概率公式。。。。。
P(A/B) = P(AB)/P(B);
代码如下:
#include <cstdio> #include <cstring> const int M = 22; double p[M]; double ans[22]; bool vis[M]; int r, n; void dfs(int k, int cur) { if(cur==0) { double t = 1; for(int i = 1; i <= n; i++) if(vis[i]) t*=p[i]; else t*=(1-p[i]); ans[0]+=t; for(int i = 1; i <= n; i++) if(vis[i]) ans[i]+=t; return ; } for(int i = k; i <= n; i++) { vis[i] = 1; dfs(i+1,cur-1); vis[i] = 0; } } int main() { int tt = 0; while(scanf("%d%d",&n,&r),r+n) { memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); for(int i = 1; i <= n; i++) scanf("%lf",&p[i]); dfs(1,r); printf("Case %d:\n",++tt); for(int i = 1; i <= n; i++) printf("%.6lf\n",ans[i]/ans[0]); } return 0; }