您的位置:首页 > 编程语言 > C语言/C++

UVa 11181 Probability|Given

2016-11-08 15:17 323 查看
N friends go to the local super market together. The probability of their buying something from the

market is p1, p2, p3, . . . , pN 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 pi

(0.1 < pi < 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

3 2

0.10

0.20

0.30

5 1

0.10

0.10

0.10

0.10

0.10

0 0

Sample Output

Case 1:

0.413043

0.739130

0.847826

Case 2:

0.200000

0.200000

0.200000

0.200000

0.200000

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dfs+概率~

模拟从n个人中选m个人,每次乘上这m个人买了东西的概率,然后再乘上剩下人没有买东西的概率,最后加在和上,一定要乘没有买东西的概率!

这两个搜索应该可以合成一个,但是我是按两个写的~

#include<cstdio>

int n,m,cnt;
double tot,a[2001],flag,now,tot2;

void findd(int u,int v)
{
if(u==m)
{
if(v<=n) for(int i=v;i<=n;i++) now*=(1-a[i]);
tot+=now;return;
}
for(int i=v;i<=n-m+u+1;i++)
{
double kkz=now;
for(int j=v;j<i;j++) now*=(1-a[j]);
now*=a[i];findd(u+1,i+1);now=kkz;
}
}

void findd2(int u,int v,int kkz)
{
if(u==m)
{
if(v<=n) for(int i=v;i<=n;i++) if(i!=kkz) now*=(1-a[i]);
tot2+=now;return;
}
if(v>=n+1) return;
for(int i=v;i<=n-m+u+2;i++)
if(i!=kkz)
{
double kkzv=now;
for(int j=v;j<i;j++)
if(j!=kkz) now*=(1-a[j]);
now*=a[i];findd2(u+1,i+1,kkz);now=kkzv;
}
}

int main()
{
while(scanf("%d%d",&n,&m)==2 && n)
{
flag=tot=0;
for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
now=1;findd(0,1);
printf("Case %d:\n",++cnt);
for(int i=1;i<=n;i++)
{
tot2=0;now=a[i];findd2(1,1,i);
printf("%.6lf\n",tot2/tot);
}
for(int i=1;i<=n;i++) a[i]=0;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ dfs 概率