当前位置:网站首页>【剑指 Offer】 60. n个骰子的点数
【剑指 Offer】 60. n个骰子的点数
2022-07-06 10:11:00 【LuZhouShiLi】
剑指 Offer 60. n个骰子的点数
题目
把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s的所有可能的值出现的概率。你需要用一个浮点数数组返回答案,其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。
思路
- 首先用数组的第一维表示阶段,也就是投掷完几个骰子
- 然后用数组的第二维表示投掷完这些骰子之后,可能出现的点数
- 数组的值就表示,该阶段各个点数出现的次数
代码
class Solution {
public:
vector<double> dicesProbability(int n) {
int dp[12][70];
memset(dp,0,sizeof(dp));// 全部初始化0
for(int i = 1; i <= 6; i++)
{
dp[1][i] = 1;// 状态数组初始化
}
for(int i = 2; i <= n; i++)
{
// 最大点数 一定是 6 * i
for(int j = i; j <= 6 * i; j++)
{
for(int cur = 1; cur <= 6; cur++)
{
if(j - cur <= 0)
{
break;
}
// 但看第i个骰子,点数可能为1 2 3 4 5 6 因此点数j出现的次数是由投掷完第 i - 1个骰子 对应点数 j - 1 j - 2 j - 6 出现的次数之和转化过来的
dp[i][j] += dp[i - 1][j - cur];
}
}
}
int all = pow(6,n);// 所有可能性
vector<double> ret;
for(int i = n; i <= n * 6; i++)
{
ret.push_back(dp[n][i] * 1.0 / all);
}
return ret;
}
};
边栏推荐
- MSF horizontal MSF port forwarding + routing table +socks5+proxychains
- DNS hijacking
- Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
- STM32按键状态机2——状态简化与增加长按功能
- Manifest of SAP ui5 framework json
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
- Nodejs 开发者路线图 2022 零基础学习指南
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- 带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
- Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
猜你喜欢
C语言通过指针交换两个数
2019 Alibaba cluster dataset Usage Summary
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
面试突击63:MySQL 中如何去重?
10 advanced concepts that must be understood in learning SQL
Awk command exercise
Jerry's updated equipment resource document [chapter]
【Swoole系列2.1】先把Swoole跑起来
JMeter interface test response data garbled
F200——搭载基于模型设计的国产开源飞控系统无人机
随机推荐
基于STM32+华为云IOT设计的智能路灯
推荐好用的后台管理脚手架,人人开源
IP, subnet mask, gateway, default gateway
關於這次通信故障,我想多說幾句…
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
declval(指导函数返回值范例)
OliveTin能在网页上安全运行shell命令(上)
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
Codeforces Round #803 (Div. 2)
Jerry's updated equipment resource document [chapter]
JMeter interface test response data garbled
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
李书福为何要亲自挂帅造手机?
ASEMI整流桥DB207的导通时间与参数选择
编译原理——自上而下分析与递归下降分析构造(笔记)
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Unity particle special effects series - treasure chest of shining stars
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)