hdu 4586 Play the Dice 概率推导题

A - Play the Dice
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87326#problem/A

Description

There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th side is up, you will get ai yuan. What's more, some sids of this dice are colored with a special different color. If you turn this side up, you will get once more chance to roll the dice. When you roll the dice for the second time, you still have the opportunity to win money and rolling chance. Now you need to calculate the expectations of money that we get after playing the game once.

Input

Input consists of multiple cases. Each case includes two lines. 
The first line is an integer n (2<=n<=200), following with n integers a i(0<=a i<200) 
The second line is an integer m (0<=m<=n), following with m integers b i(1<=b i<=n), which are the numbers of the special sides to get another more chance.

Output

Just a real number which is the expectations of the money one can get, rounded to exact two digits. If you can get unlimited money, print inf. 

Sample Input

6 1 2 3 4 5 6
0
4 0 0 0 0
1 3

Sample Output

3.50
0.00 

HINT

题意

一个n面的骰子,每一面有分值,并且扔到某些面的时候,可以再扔一次,然后问你最后得分的期望是多少

题解

  设期望值为s,前m个是再来一次机会,则有

  s=(a[1]+s)/n+(a[2]+s)/n+……+(a[m]+s)/n+a[m+1]/n……

   化简:(n-m)s=sum

   当sum=0时,为0;

    当n==m时,为inf;

    否则为sum/(n-m).

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//***************************

int main()
{
    int n;
    int hh;
    while(cin>>n)
    {
        double sum=0;
        double a[500];
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            sum+=a[i];
        }
        int m;
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            hh=read();
        }
        if(sum==0)cout<<0<<endl;
        else 
        if(n==m)cout<<"inf"<<endl;
        else
        printf("%.2f
",sum/(n-m));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zxhl/p/4734269.html