LightOJ - 1030 Discovering Gold (期望)

LightOJ - 1030 Discovering Gold (期望)

題目鏈接: LightOJ-1030
題目大意:
n 個格子,每個格子有ai 的金子,初始位置爲1,操作是扔六面骰子,扔幾點走幾步,每到達一個新位置,就得到該位置的金子。到n 的時候停止,如果下次移動的位置超過n ,重新投擲骰子直到合法位置。求所能得到金子數量的期望。

解題思路:
dp[i] 表示從i 位置開始的期望, 對於一個位置的i 的期望,dp[i]=i+6j=i+1dp[j]6 , 當然後面不夠6個的時候處理一下就好。

AC代碼:

/********************************************
 *Author*        :��ZZZZone
 *Created Time*  : 五 10/13 20:20:24 2017
 * Ended  Time*  : 五 10/13 20:24:37 2017
*********************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
const int MaxN = 100;
double dp[MaxN + 5];
int T, n, Case;

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%lf", &dp[i]);
        for(int i = n - 1; i >= 1; i--){
            double tot = 0;
            int cnt = min(6, n - i);
            for(int j = i + 1; j <= i + cnt; j++) tot += dp[j];
            dp[i] += tot / cnt;
        }
        printf("Case %d: %lf\n", ++Case, dp[1]);
    }

    return 0;
}

在此輸入正文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章