当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- 分布式不来点网关都说不过去
- TCP packet sticking problem
- Principle and usage of extern
- 传统家装有落差,VR全景家装让你体验新房落成效果
- Distinguish between basic disk and dynamic disk RAID disk redundant array
- VR全景婚礼,帮助新人记录浪漫且美好的场景
- 關於這次通信故障,我想多說幾句…
- The easycvr authorization expiration page cannot be logged in. How to solve it?
- 面试突击63:MySQL 中如何去重?
- 基本磁盘与动态磁盘 RAID磁盘冗余阵列区分
猜你喜欢

Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
![[Android] kotlin code writing standardization document](/img/d5/53d6a75e87af15799bf7e5d6eb92a5.png)
[Android] kotlin code writing standardization document

开源与安全的“冰与火之歌”

Getting started with pytest ----- test case rules

Getting started with pytest ----- test case pre post, firmware

Four processes of program operation

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design

Awk command exercise

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
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Pytest learning ----- pytest confitest of interface automation test Py file details
Shell input a string of numbers to determine whether it is a mobile phone number
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
Interview assault 63: how to remove duplication in MySQL?
std::true_type和std::false_type
Getting started with pytest ----- test case pre post, firmware
Declval (example of return value of guidance function)
d绑定函数
Jerry's watch reads the file through the file name [chapter]
2022暑期项目实训(二)
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Appium automated test scroll and drag_ and_ Drop slides according to element position
Nodejs developer roadmap 2022 zero foundation Learning Guide
Markdown grammar - better blogging