dp[i]表示,从i点出发到结束位置,期望获得的黄金。
dp[i] = val[i] + ∑dp[j] / cnt(j是i能一步到达的位置,cnt是一步能到达的位置的数量)
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 ",cas,dp[1]); 28 } 29 }