栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

UVA11181 Probability|Given【概率+DFS】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UVA11181 Probability|Given【概率+DFS】

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

问题链接:UVA11181 Probability|Given
问题简述:(略)
问题分析:概率计算问题,不解释。解题代码来自仙客传奇团队。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:


#include 
#include 
#include 

const int N = 20 + 1;
double ans[N], p[N];
int n, r;

double dfs(int cur, int cnt, double pp)   //通过递归来枚举各种情况
{
    if (cur > n) return cnt? 0:pp;

    double sum=0;
    if (cnt) {
        sum += dfs(cur + 1, cnt - 1, pp * p[cur]);
        ans[cur] += sum;
    }
    sum += dfs(cur + 1, cnt, pp * (1 - p[cur]));
    return sum;
}

int main()
{
    int caseno = 0;
    while (~scanf("%d%d", &n, &r) && (n || r)) {
        for (int i = 1; i <= n; i++)
            scanf("%lf",&p[i]);

        memset(ans, 0, sizeof(ans));
        double all = dfs(1, r, 1);

        printf("Case %d:n", ++caseno);
        for (int i = 1; i <= n; i++)
            printf("%fn", ans[i] / all); //p(a|b)=p(ab)/p(b)
    }

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号