hdu-Play the Dice解题报告
Problem 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
Source
2013 ACM-ICPC南京赛区全国邀请赛——题目重现
题目大意:
有一个骰子有n个面,每一面都有对应的一个值,当摇到一个面时(摇到每个面的概率相等),便可以获得相应值得钱。其中有m个面是有特殊颜色的,当摇一次骰子之后,如果摇到有特殊颜色的面,则可以获得再摇一次的机会。现在要求获得的总的钱的期望,(如果可以获得无限多的钱则输出inf)。
这段时间做的比赛当中,经常会出现求期望的题目,不知道是不是学长故意挑的。(挺蛋疼的.....)
解题思路:
刚开始想了很久,看见有很多人都把这题给AC了,心里挺着急的。大概过了两个多小时之后,队友才把公式给推出来。(看来还得恶补数学了,哎)。
设数学期望为EX,骰子的n个面的值分别为a1,a2,a3.......an。
当骰子摇到有特殊颜色的那些面的时候,又可以摇一次,这时就相当于重新计算。
所以公式为:EX=( a1 ) / n + ( a2 ) / n +..........+ ( ai + EX ) / n +.....+ ( aj + EX ) / n +.....+ ( an ) / n
化简得到: EX=( a1+a2+a3+.....+an ) / n + ( m*EX ) / n
得到:EX=(a1+a2+a3+.....+an) / (n-m)
当n==m时,则可以得到无限多的钱;当分子等于0时,期望也为0;
#include<stdio.h>
int main()
{
int n,m,i,j,sum;
double Ex;
int a[220],b[220];
while(~scanf("%d",&n))
{
sum=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
if(sum==0)
printf("0.00\n");
else if(n==m)
printf("inf\n");
else
{
Ex=(double)(sum)/(double)(n-m);
printf("%.2lf\n",Ex);
}
}
return 0;
}