LightOJ 1030 Discovering Gold(期望)

Description

You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.

Output

For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6will be ignored.

Sample Input

Sample Output
Case : 101.0000000000
Case : 13.000
Case :

题目链接:http://lightoj.com/volume_showproblem.php?problem=1030

*********************************************

题意:给出T 组实例,每组实例给出n,接着n个数,分别代表i位置有黄金的数量。你掷骰子前进,掷到几就走几步,如果即将到达的位置存在的话。走到了哪个位置就可以得到那个位置的黄金。让你求你到达最后一个位置时收集黄金的期望。

分析:dp保存没点的概率,注意骰子只有6步,所以注意大于6的情况;

数学期望 = 每一点的概率 * 到该点的值。

AC代码:

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 12000
#define INF 0x3f3f3f3f double dp[N];
int a[N]; int main()
{
int n,T,k=,i,j,x; scanf("%d", &T); while(T--)
{
scanf("%d", &n);
memset(dp,,sizeof(dp));
double sum=0.0; for(i=;i<=n;i++)
scanf("%d", &a[i]); dp[]=; for(i=;i<=n;i++)
{
x=(n-i<?n-i:);
for(j=;j<=x;j++)
dp[i+j]+=dp[i]*1.0/x; sum+=dp[i]*a[i];
} printf("Case %d: %.10f\n", k++,sum);
} return ;
}
上一篇:Spring 源码学习:day1


下一篇:Yum Proxy