• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 4586 Play the Dice (数学,概率,等比公式,极限)

题意:给你一个n面的骰子每个面有一个值,然后其中有不同值代表你能获得的钱,然后有m个特殊的面,当你骰到这一面的时候可以获得一个新的机会

问你能得到钱的期望。

析:

骰第一次     sum/n

骰第二次     sum/n*(m/n)

骰第三次     sum/n*(m/n)*(m/n)

骰第四次     sum/n*(m/n)*(m/n)*(m/n)

............

骰第k次     sum/n*(m/n)^k

即    sum/n*(1+q+q^2+q^3+……+q^k)    q=m/n  k=inf    

所以           sum/n*(1/(1-m/n))  =  (sum/n)*(n/n-m)  =sum/(n-m)

那么 n=m时候期望无限大

notice sum等于0的时候直接输出 0.00

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 200 + 5;
const long double eps = 1e-1000;
int a[maxn];

int main(){
    int n, m;
    while(scanf("%d", &n) == 1){
        int sum = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
            sum += a[i];
        }

        scanf("%d", &m);
        int x;
        for(int i = 0; i < m; ++i)
            scanf("%d", &x);

        if(!sum){
            printf("%.2lf\n", 0.0);
            continue;
        }
        else if(sum && m == n){
            printf("inf\n");
            continue;
        }

        printf("%.2lf\n", sum*1.0/(n-m));

    }
    return 0;
}

 

posted on 2016-07-27 19:21  dwtfukgv  阅读(193)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3