您的位置:首页 > 其它

C. Little Pony and Expected Maximum

2014-08-17 23:46 447 查看
这道题目的大意是,一个骰子有6个面分别写着1,2,3,4,5,6出现的概率相同,然后你可以扔n次,要求的是平均下来,可能的最大的数目。什么意识?比如一个两面的骰子,你可以投两次,下面是投的两次的几种可能:

1. You can get 1 in the first toss, and 2 in the second.Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second.Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second.Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second.Maximum equals to 2
然后呢你就可以有:The probability of each outcome is 0.25, that is expectationequals to:

(2+1+2+2)*0.25(也就是1/4每一种情况是十分之一概率)。
上面标注的是组合数学,数学,的确是数学,但是运用组合数学的知识我并没有解出来,后来还是找规律写出来的,对于一个特定的面数的骰子,每一种出现的可能次数是固定的,比如说6面,一共36种情况那么:1最大的是1种,2最大的是上面三种,三最大的是5种(9-1-3)为什么?因为3最大就不需要考虑4,5,6了,所以就可以是3个面的了,一共是9次,那么4也是一样的,也就是他的这个次数和面数以及次数有一个平行走向的关系。

在这一题目中如果有幂次的运用如果你用循环就会超时,所以还是用pow函数直接AC

#include<iostream>
using namespace std;
#include<cstring>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
int m,n;
/*
double mi(int M,int n)
{
    double k1=1.0,k2=1.0;
    for(int i=1;i<=n;i++)
        k1*=M*1.0/m;
    for(int i=1;i<=n;i++)
            k2*=(M-1)*1.0/m;
    return k1-k2;
}
*/
int main()
{
    int i,j,k;
    double sum;
    while(cin>>m>>n)
    {
        sum=0;
        for(i=1;i<=m;i++)
        {
           // cout<<mi(i,n)*i<<endl;
            sum+=(pow(i*1.0/m,n)-pow((i-1)*1.0/m,n))*i;
        }
    printf("%.12lf\n",sum);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: