活跃农民
- 积分
- 491
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-7-15
- 最后登录
- 1970-1-1
|
最后一道题,我也不知道怎么做。感觉和markhov chain有关,但是linear algebra早忘光了。。。所以就写了段code来模拟运行,最后答案是p=1/7,所以20p约等于3。也不知道写的algorithm对不对。先把code po在这里,抛砖引玉一下。
- #probs[i] stands for the probability of the sum of two threw dice being i
- probs = [0.0,0.0,1.0/36,2.0/36,3.0/36,4.0/36,5.0/36,6.0/36,5.0/36,4.0/36,3.0/36,2.0/36,1.0/36]
- # use dynamic programming to figure out
- FINAL_GRID_NUM = 40*11
- dp = [0.0]*(FINAL_GRID_NUM+1)
- dp[1] = 1.0 #the starting point
- # x stands for the grid number
- # i stands for the throwing sum
- for x in range(3,441):
- for i in range(2,13):
- if x>i: dp[x] += probs[i]*dp[x-i]
- print(20*dp[FINAL_GRID_NUM])
复制代码 |
|