当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- Appium automated test scroll and drag_ and_ Drop slides according to element position
- sql语句优化,order by desc速度优化
- 传统家装有落差,VR全景家装让你体验新房落成效果
- OpenEuler 会长久吗
- 面向程序员的精品开源字体
- How to use scroll bars to dynamically adjust parameters in opencv
- 带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
- UDP协议:因性善而简单,难免碰到“城会玩”
- kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
猜你喜欢

FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog

After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer

SAP UI5 框架的 manifest.json

std::true_type和std::false_type

scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月

带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities

面向程序员的精品开源字体

10 advanced concepts that must be understood in learning SQL

传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复

8位MCU跑RTOS有没有意义?
随机推荐
Interview shock 62: what are the precautions for group by?
node の SQLite
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Jerry's watch deletes the existing dial file [chapter]
MarkDown语法——更好地写博客
C语言通过指针交换两个数
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
Open source and safe "song of ice and fire"
The difference between parallelism and concurrency
Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
DNS hijacking
Jielizhi obtains the currently used dial information [chapter]
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
【Android】Kotlin代码编写规范化文档
8位MCU跑RTOS有没有意义?
Reppoints: advanced order of deformable convolution
關於這次通信故障,我想多說幾句…
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
Declval of template in generic programming