Lightoj 1030 discovering gold[expectations]

Source: Internet
Author: User

B-Discovering Gold

Description

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

Initially you is in position 1. Now the turn you throw a perfect6 sided dice. If you get x at the dice after throwing, you addx to your position and collect all the gold from the new Position. If your new position is outside the cave and then you keep throwing again until you get a suitable result. When you reach theNth position you stop your journey. Now is given the information about the cave and you have to find out the theexpected number of gold 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 containsN space separated integers. The ith integer of this line denotes the amount of gold you'll get if you come to theith cell. Safely assume that all the given integers would be non-negative and no integer would be greater than.

Output

For each case, print the case number and the expected number of gold you'll collect. Errors less than10-6 'll be ignored.

Sample Input

3

1

101

2

10 3

3

3 6 9

Sample Output

Case 1:101

Case 2:13

Case 3:15


Main topic: From 1 position to roll the dice, to n position, each position has the right value, seeking the last to obtain the weight expectation;


Code:

Posture One: yy disorderly engage;

Code:

int v[110];d ouble dp[110][110];d ouble f[110];int main () {int t,cas=0;    scanf ("%d", &t);        while (t--) {int n;        scanf ("%d", &n);        for (int i=1;i<=n;i++) scanf ("%d", v+i);            for (int i=0;i<=n;i++) {f[i]=0.0;        for (int j=0;j<=n;j++) dp[i][j]=0.0;        } dp[1][1]=1.0;        f[1]=1.0;        int flag;                for (int i=2;i<=n;i++) {for (int j=1;j<=n-1;j++) {flag=0;                    for (int k=6;k>=1;k--) {if (!flag&&j+k<=n) {flag=k;                    } if (flag) {Dp[i][j+k]+=dp[i-1][j]/flag;        }}} for (int j=1;j<=n;j++) F[J]+=DP[I][J];        } double ans=0.0;            for (int i=1;i<=n;i++) {//cout<< "-" <<f[i]<<endl;        Ans+=f[i]*v[i]; } priNTF ("Case%d:%.10lf\n", ++cas,ans); } return 0;}

Posture Two: Dp[i] represents the weight I to N to obtain the weight of the expected dp[i]=v[i]+dp[i+k]/j;

Code:

int v[110];d ouble dp[110];int Main () {    int t,cas=0;    scanf ("%d", &t);    while (t--)    {        int n;        scanf ("%d", &n);        for (int i=1;i<=n;i++)            scanf ("%d", v+i);        Dp[n]=v[n];        for (int i=n-1;i>=1;i--) {            int k=min (6,n-i);            Dp[i]=v[i];            for (int j=1;j<=k;j++) {                dp[i]+=dp[i+j]/k;            }        }        printf ("Case%d:%.10f\n", ++cas,dp[1]);    }    return 0;}

Posture Three: dp[i] after the probability of the position I, finally seek ans;

Code:

int v[110];d ouble dp[110];int Main () {    int t,cas=0;    scanf ("%d", &t);    while (t--)    {        int n;        scanf ("%d", &n);        for (int i=1;i<=n;i++) {            dp[i]=0.0;            scanf ("%d", v+i);        }        Dp[1]=1;        for (int i=1;i<=n-1;i++) {            int k=min (6,n-i);            for (int j=1;j<=k;j++)                dp[i+j]+=dp[i]/k;        }        Double ans=0.0;        for (int i=1;i<=n;i++)            ans+=dp[i]*v[i];        printf ("Case%d:%.10f\n", ++cas,ans);    }    return 0;}




Lightoj 1030 discovering gold[expectations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.