lightoj 1030 Discovering Gold expectation

dp [i] represents, from the point i to the end, expect to get the gold.

dp [i] = val [i] + Σdp [j] / cnt (j is the position of the step i can reach, cnt is the number of positions that can be reached step)

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int T,cas,n;
 5 int val[105];
 6 double dp[105];
 7 int main()
 8 {
 9     for (scanf("%d",&T);T != 0;T--)
10     {
11         cas++;
12         scanf("%d",&n);
13         for (int i = 1;i <= n;i++)
14             scanf("%d",&val[i]);
15         dp[n] = val[n];
16         for (int i = n - 1;i >= 1;i--)
17         {
18             double sum = 0;
19             int cnt = 0;
20             for (int j = i + 1;j <= min(n,i + 6);j++)
21             {
22                 sum += dp[j];
23                 cnt++;
24             }
25             dp[i] = val[i] + sum / cnt;
26         }
27         printf("Case %d: %.6lf\n",cas,dp[1]);
28     }
29 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11410270.html