题目链接:https://vjudge.net/problem/LightOJ-1030

1030 - Discovering Gold
Time Limit: 2 second(s) Memory Limit: 32 MB

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-6 will be ignored.

Sample Input

Output for Sample Input

3

1

101

2

10 3

3

3 6 9

Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

题意:

在水平线上有n个格,每个格有价值若干的黄金。人从第一个位置开始,每次先摇骰子,然后再跳到新的格子,如果新格子在n格之外,则重新摇骰子,直到到达n。问得到的黄金的平均值。

题解:

1.可知从i跳到i+1~i+6的概率是相等的,所以:dp[i] = (dp[i+1]+……+dp[i+6])/6 + val[i]。

2.根据上述式子,可知要从后面往前递推。当然不足6的需要特殊处理。

易错点:

开始做的时候,想到的是从前往后递推:dp[i] = (dp[i-1]+……+dp[i-6])/6 + val[i] 。

然而从i-6~i-1到i的概率不一定是相等的,因为他们不在同一次抛筛子。

但是从后往前递推的话,从i跳到i+1~i+6的概率是相等的,因为都是在i的位置抛骰子决定的。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e2+; double val[MAXN], dp[MAXN];
int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%lf", &val[i]); dp[n] = val[n];
for(int i = n-; i>=; i--)
{
int Last = min(i+,n), cnt = Last - i;
dp[i] = val[i];
for(int j = i+; j<=Last; j++)
dp[i] += dp[j]/cnt;
}
printf("Case %d: %.10lf\n", ++kase, dp[]);
}
}

最新文章

  1. CSS3:radial-gradient,径向渐变的使用方法
  2. jQuery中的事件处理
  3. Unity jounal 2016-3-3
  4. Django提交POST表单“CSRF verification failed. Request aborted”问题的解决
  5. 32-bit ALU [Verilog]
  6. DOM-based xss
  7. Cocos2d-JS引入其他场景小实例
  8. C#绘图双缓冲
  9. 转: 基于nginx的hls直播系统
  10. xampp配置host和httpd可以随意访问任何本机的地址
  11. CT 来值班,让您安心过新年!
  12. iOS UIKit:animation
  13. Nagios监控memcached
  14. DataTables给表格绑定事件
  15. 数据结构c字符串操作语言版本
  16. DMO节点内部插入的常用方法与区别
  17. vue安装使用
  18. N2N windows下编译安装文件
  19. 客户端连接linux经常间隔性断开链接【转】
  20. Nginx负载均衡初识

热门文章

  1. ‘cnpm&#39; 不是内部或外部命令,也不是可运行的程序
  2. Linux字符模式下如何设置/删除环境变量
  3. 邁向IT專家成功之路的三十則鐵律 鐵律十二:IT人養生之道-德行
  4. angular md-toast 颜色
  5. C#应用程序配置文件.config介绍
  6. Win7安装软件,界面上中文显示乱码的解决方案
  7. python_获得列表中重复的项的索引
  8. 别样JAVA学习(五)继承上(1.1)Object类toString()
  9. Linux基础(1)- 命令和目录文件
  10. ACPI in Linux