C
来源:互联网 发布:网络合同纠纷判决 编辑:程序博客网 时间:2024/06/15 05:07
题目链接:点击打开链接
题意:n 个人去购物,只有 r 个人买东西,而剩下的 n - r 个人不买东西,给出每个人可能买东西的概率,让求每个人在那 r 个人中的概率;
概率公式:P(A | B)= P(AB)/ P(B) ;P(A | B)指的是在 B 发生的情况下,A 发生的概率; P(AB) 指的是A 和B 同时发生的概率,P(B)指B 发生的概率;
在本题中,有 t1,t2,t3 三人,两人买东西,以 t1为例, P(A| B)是结果,P(AB)是 3 人中 2 人买东西且 t1 的概率,P(B)指全部两人买东西的概率;
例如:
n = 3,r = 2;
t1 = 0.1 ,t2 = 0.2 , t3 = 0.3
t1: P(AB)=0.1*0.2*0.7+0.1*0.8*0.3; P(B) = 0.1*0.2*0.7+0.1*0.8*0.3+0.9*0.2*0.3 所以 P(A | B)=P(AB)/ P(B)=0.413043;
详情见代码;
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<iomanip>#include<vector>#include<algorithm>using namespace std;int main(){ int n,r; int kase=0; while(cin>>n>>r){ if(n==0&&r==0) break; double a[n+10]; for(int i=0;i<n;i++) scanf("%lf",&a[i]); string str; for(int i=0;i<n-r;i++) str+='0'; for(int i=n-r;i<n;i++) str+='1'; double num[n+10]; memset(num,0,sizeof(num)); double sum=0.0; do{ double ans=1.0; for(int i=0;i<str.length();i++){ if(str[i]=='1') ans=ans*a[i]; else ans=ans*(1-a[i]); } for(int i=0;i<n;i++){ if(str[i]=='1') num[i]+=ans; } sum+=ans; }while(next_permutation(str.begin(),str.end())); cout<<"Case "<<++kase<<":"<<endl; for(int i=0;i<n;i++){ cout<<fixed<<setprecision(6)<<num[i]/sum<<endl; } } return 0;}
阅读全文
0 0
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- flex BorderImage
- hdu 1012 constructing the road
- memcpy_s 参数问题
- 荷兰研究人员提突破性想法:用红外线传输网络_比WiFi快100倍
- AsyncTask原理
- C
- CentOS mariaDB10升级成mysql5.6
- 最长上升子序列
- PLS-00221: 'function' 不是过程或尚未定义
- Mongo基础知识笔记
- 动态规划之状态压缩dp入门
- Unity开发游戏优化相关
- 处理器核心 错误源: 已更正的计算机检查 错误类型: 内部奇偶校验错误
- java基础知识简单算法题目