zoukankan      html  css  js  c++  java
  • CSU 【抛硬币】

    Description

    James得到了一堆有趣的硬币,于是决定用这些硬币跟朋友们玩个小游戏。在一个N行M列的表格上,每一个第i行第j列的格子上都放有一枚James的硬币,抛该硬币正面朝上的概率为Pij,所有抛硬币事件两两之间是相互独立的。

    现在,玩家在M列硬币中,从每一列里各选择1枚,共M枚,构成一组。如此重复选择N组出来,且保证被选择过的硬币不能再选。选好组之后,每组的M枚硬币各抛一次,如果都是正面朝上,则该组胜利,总分赢得1分;否则该组失败,总分不加也不减。请问,如果让你自行选择硬币的分组,游戏总得分的数学期望的最大值是多少?

    Input

    输入有多组数据。每组数据第一行为N和M,1≤N≤100,1≤M≤10,以空格分隔。接下来有N行,每行M个小数,表示表格中对应的Pij

    输入以N=M=0结束,这组数据不输出结果。

    Output

    对于每组数据,输出对应游戏总得分的数学期望的最大值,四舍五入精确至4位小数。每组数据的输出占一行。

    Sample Input

    2 3
    1.0 1.0 1.0
    0.5 0.4 0.3
    0 0
    

    Sample Output

    1.0600

    题解:将概率的二维数组按列排序做出贪心选择即可。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    using namespace std;
    const int maxn=105;
    double p[maxn][maxn];
    int n,m;
    int main()
    {
        while(scanf("%d %d",&n,&m)){
            double EX=0;
            if(n==0||m==0)
                break;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    scanf("%lf",&p[i][j]);
                }
            }
            double t;
            for(int j=0;j<m;j++){
                for(int i=0;i<n;i++){
                    for(int k=i+1;k<n;k++){
                        if(p[k][j]>p[i][j]){
                            t=p[k][j];
                            p[k][j]=p[i][j];
                            p[i][j]=t;
                        }
                    }
                }
            }
            for(int i=0;i<n;i++){
                t=1;
                for(int j=0;j<m;j++){
                    t*=p[i][j];
                }
                EX+=t;
            }
            printf("%.4lf
    ",EX);
        }
        return 0;
    }
    

  • 相关阅读:
    设计模式- 模板方法模式
    什么是Coded UI
    请介绍WCF服务
    我的WCF之旅(1):创建一个简单的WCF程序
    7.3 Models -- Creating And Deleting Records
    7.2 Models -- Defining Models
    7.1 Models -- Introduction
    6.3 Controllers -- Managing Dependencies Between Controllers
    6.2 Controllers -- Representing Multipe Models
    6.1 Controllers -- Introduction
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205215.html
Copyright © 2011-2022 走看看