当前位置:网站首页>[sword finger offer] 60 Points of N dice
[sword finger offer] 60 Points of N dice
2022-07-06 18:18:00 【LuZhouShiLi】
The finger of the sword Offer 60. n The number of dice
subject
hold n A dice on the ground , The sum of the points on the up side of all dice is s. Input n, Print out s The probability that all possible values of the . You need to return the answer with an array of floating-point numbers , Among them the first i The elements represent this n The number of points that a die can roll i The probability of the smaller one .
Ideas
- First, use the first dimension of the array to represent the stage , That is, throw a few dice
- Then use the second dimension representation of the array to roll these dice , Possible points
- The value of the array represents , The number of points in this stage
Code
class Solution {
public:
vector<double> dicesProbability(int n) {
int dp[12][70];
memset(dp,0,sizeof(dp));// All initialization 0
for(int i = 1; i <= 6; i++)
{
dp[1][i] = 1;// Status array initialization
}
for(int i = 2; i <= n; i++)
{
// The maximum number of points It must be 6 * i
for(int j = i; j <= 6 * i; j++)
{
for(int cur = 1; cur <= 6; cur++)
{
if(j - cur <= 0)
{
break;
}
// But look No i A dice , Points may be 1 2 3 4 5 6 So points j The number of occurrences is determined by the end of the throw i - 1 A dice Corresponding points j - 1 j - 2 j - 6 The sum of the number of occurrences is transformed
dp[i][j] += dp[i - 1][j - cur];
}
}
}
int all = pow(6,n);// All possibilities
vector<double> ret;
for(int i = n; i <= n * 6; i++)
{
ret.push_back(dp[n][i] * 1.0 / all);
}
return ret;
}
};
边栏推荐
- On time and parameter selection of asemi rectifier bridge db207
- 【Swoole系列2.1】先把Swoole跑起来
- UDP协议:因性善而简单,难免碰到“城会玩”
- Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery
- [.Net core] solution to error reporting due to too long request length
- Insert dial file of Jerry's watch [chapter]
- Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
- 重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
- I want to say more about this communication failure
- 转载:基于深度学习的工业品组件缺陷检测技术
猜你喜欢
随机推荐
Why does wechat use SQLite to save chat records?
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
OpenEuler 会长久吗
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
C语言通过指针交换两个数
Codeforces Round #803 (Div. 2)
Virtual machine VirtualBox and vagrant installation
Grafana 9.0 is officially released! It's the strongest!
[.Net core] solution to error reporting due to too long request length
阿里云国际版ECS云服务器无法登录宝塔面板控制台
std::true_type和std::false_type
2022 Summer Project Training (II)
华为0基金会——图片整理
图片缩放中心
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
SQL优化问题的简述
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
MarkDown语法——更好地写博客
F200——搭载基于模型设计的国产开源飞控系统无人机








