当前位置:网站首页>[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;
}
};
边栏推荐
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- Jerry's watch reading setting status [chapter]
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- 二分(整数二分、实数二分)
- Markdown grammar - better blogging
- Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
- atcoder它A Mountaineer
- 面向程序员的精品开源字体
- C语言通过指针交换两个数
- Easy to use PDF to SVG program
猜你喜欢
Compilation Principle -- C language implementation of prediction table
UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"
On time and parameter selection of asemi rectifier bridge db207
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
面向程序员的精品开源字体
小程序在产业互联网中的作用
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
J'aimerais dire quelques mots de plus sur ce problème de communication...
Jerry's access to additional information on the dial [article]
TOP命令详解
随机推荐
Compilation Principle -- C language implementation of prediction table
AFNetworking框架_上传文件或图像server
declval(指导函数返回值范例)
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
2022暑期项目实训(三)
【Android】Kotlin代码编写规范化文档
Alibaba cloud international ECS cannot log in to the pagoda panel console
C语言通过指针交换两个数
關於這次通信故障,我想多說幾句…
Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
POJ 2208 已知边四面体六个长度,计算体积
Declval (example of return value of guidance function)
2019 Alibaba cluster dataset Usage Summary
celery最佳实践
Markdown grammar - better blogging
DOM简要
High precision operation
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
C language exchanges two numbers through pointers