首页 > 代码库 > UVA&&POJ离散概率练习[3]

UVA&&POJ离散概率练习[3]

POJ3869 Headshot

题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot

条件概率,|00|/(|00|+|01|)和|0|/n谁大的问题

|00|+|01|=|0|

注意序列是环形

 

////  main.cpp//  poj3869////  Created by Candy on 25/10/2016.//  Copyright © 2016 Candy. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N=105;int n,a,b;char s[N];int main(int argc, const char * argv[]) {    scanf("%s",s+1);    n=strlen(s+1);    for(int i=1;i<=n;i++){        if(s[i]==0) b++;        if(s[i]==0&&s[i+1]==0) a++;    }    if(s[n]==0&&s[1]==0) a++;    if(a*n>b*b) printf("SHOOT");    else if(a*n==b*b) printf("EQUAL");    else printf("ROTATE");    return 0;}

 




UVA - 10491

Cows and Cars

 

经典问题,a奶牛,b车,c门展示

全概率公式,分成一开始选了牛a/(a+b)和一开始选了车b/(a+b)两部分

部分里总是换门得到车概率分别是b/(a+b-c-1)和(b-1)/...

 

#include <cstdio>double a,b,c;int main(int argc, const char * argv[]) {    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)        printf("%.5f\n",(a*b+b*(b-1))/((a+b)*(a+b-c-1)));    return 0;}

 




UVA - 11181
Probability|Given

 

题意:n个人,买东西概率pi,有r个人买了东西,求每个人实际买东西概率

条件概率

E为r个人买东西,Ei为r个人中有i买东西

P(Ei|E)=P(EiE)/P(E)

计算概率用dfs爆搜每个人买还是不买即可

PS:不要读入优化,浮点数

////  main.cpp//  uva11181////  Created by Candy on 25/10/2016.//  Copyright © 2016 Candy. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N=25;typedef long long ll;inline int read(){    char c=getchar();int x=0,f=1;    while(c<0||c>9){if(c==-)f=-1;c=getchar();}    while(c>=0&&c<=9){x=x*10+c-0;c=getchar();}    return x*f;}int n,r,vis[N];double p[N],pe[N];void dfs(int d,int cnt,double prob){//printf("dfs %d %d %f\n",d,cnt,prob);    if(cnt>r||d-1-cnt>n-r) return;    if(d==n+1){        if(cnt==r)            for(int i=1;i<=n;i++) if(vis[i]) pe[i]+=prob;        pe[0]+=prob;        return;    }    vis[d]=1;    dfs(d+1,cnt+1,prob*p[d]);    vis[d]=0;    dfs(d+1,cnt,prob*(1-p[d]));}int main(int argc, const char * argv[]){    int cas=0;    while((n=read())){printf("Case %d:\n",++cas);        r=read();        for(int i=1;i<=n;i++) scanf("%lf",&p[i]);        memset(vis,0,sizeof(vis));        memset(pe,0,sizeof(pe));        dfs(1,0,1.0);        for(int i=1;i<=n;i++) printf("%.6f\n",pe[i]/pe[0]);    }        return 0;}

 

UVA&&POJ离散概率练习[3]