当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- 分布式不来点网关都说不过去
- Transfer data to event object in wechat applet
- Summary of Android interview questions of Dachang in 2022 (I) (including answers)
- Pytorch extract middle layer features?
- sql语句优化,order by desc速度优化
- 趣-关于undefined的问题
- Getting started with pytest ----- test case pre post, firmware
- Shell input a string of numbers to determine whether it is a mobile phone number
- 【Swoole系列2.1】先把Swoole跑起来
猜你喜欢

STM32按键状态机2——状态简化与增加长按功能

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

Unity particle special effects series - treasure chest of shining stars

kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)

關於這次通信故障,我想多說幾句…

MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...

In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way

C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例

Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"

Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
随机推荐
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
The shell generates JSON arrays and inserts them into the database
Principle and usage of extern
Alibaba brand data bank: introduction to the most complete data bank
传统家装有落差,VR全景家装让你体验新房落成效果
Mysqlimport imports data files into the database
【Android】Kotlin代码编写规范化文档
std::true_type和std::false_type
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
[Android] kotlin code writing standardization document
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
QT中Model-View-Delegate委托代理机制用法介绍
Jielizhi obtains the currently used dial information [chapter]
分布式不来点网关都说不过去
《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
The difference between parallelism and concurrency
二分(整数二分、实数二分)
Awk command exercise
面向程序员的精品开源字体
2019 Alibaba cluster dataset Usage Summary