zoukankan      html  css  js  c++  java
  • UVA 11181 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

    题意:  给你n个人买东西的概率p[i] ,   问你恰好有R个人买了东西,第i个人买东西的概率

    题解: N的范围小  20

             暴力搜索所有情况就好了

       注意答案是  ans[i]/p   p为n个人与r个人买了东西的概率

    //meek///#include<bits/stdc++.h>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include<iostream>
    #include<bitset>
    using namespace std ;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define fi first
    #define se second
    #define MP make_pair
    typedef long long ll;
    
    const int N = 200;
    const int M = 1000001;
    const int inf = 0x3f3f3f3f;
    const int MOD = 100003;
    const double eps = 0.000001;
    
    double ans[N],p[N];
    int n;
    double dfs(int cnt,int r,double pi) {
        if(cnt > n)
            if(r) return 0;
            else return pi;
        double sum = 0;
        if(r) {
            sum += dfs(cnt+1,r-1,pi*p[cnt]);
            ans[cnt] += sum;
        }
        sum += dfs(cnt+1,r,pi*(1-p[cnt]));
        return sum;
    }
    int main() {
        int cas = 1,r;
        while(~scanf("%d%d",&n,&r)) {
            if(n==0&&r==0) break;
            for(int i=1;i<=n;i++) scanf("%lf",&p[i]);
            mem(ans);
            printf("Case %d:
    ", cas++);
            double p = dfs(1,r,1);
            for(int i=1;i<=n;i++)
                printf("%.6f
    ", ans[i]/p);
        }
        return 0;
    }
    代码
  • 相关阅读:
    093孤荷凌寒自学第179天区块链093天Dapp048
    092孤荷凌寒自学第178天区块链092Dapp047
    091孤荷凌寒自学第177天区块链091天Dapp046
    090孤荷凌寒自学第180天区块链090天Dapp045
    089孤荷凌寒自学第175天区块链089天Dapp044
    088孤荷凌寒自学第174天区块链088天Dapp043
    087孤荷凌寒自学第173天区块链087天Dapp042
    孤荷凌寒自学第172天区块链086天Dapp041
    孤荷凌寒自学第171天区块链085天Dapp040
    iOS开发——高级篇——内存分析,Instruments
  • 原文地址:https://www.cnblogs.com/zxhl/p/5075658.html
Copyright © 2011-2022 走看看