当前位置:网站首页>[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;
}
};
边栏推荐
- Reprint: defect detection technology of industrial components based on deep learning
- Easy to use PDF to SVG program
- 2022 Summer Project Training (II)
- Kill -9 system call used by PID to kill process
- bonecp使用数据源
- Declval of template in generic programming
- The difference between parallelism and concurrency
- 带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
- 2019阿里集群数据集使用总结
- On time and parameter selection of asemi rectifier bridge db207
猜你喜欢

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

Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)

UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"

Olivetin can safely run shell commands on Web pages (Part 1)

Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti

微信为什么使用 SQLite 保存聊天记录?

2019阿里集群数据集使用总结

win10系统下插入U盘有声音提示却不显示盘符

Rb157-asemi rectifier bridge RB157

Interesting - questions about undefined
随机推荐
Alibaba cloud international ECS cannot log in to the pagoda panel console
【Swoole系列2.1】先把Swoole跑起来
Four processes of program operation
Cobra 快速入门 - 专为命令行程序而生
1700C - Helping the Nature
2019阿里集群数据集使用总结
队列的实现
Excel usage record
SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
STM32+HC05串口蓝牙设计简易的蓝牙音箱
Reprint: defect detection technology of industrial components based on deep learning
简单易用的PDF转SVG程序
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
Maixll dock camera usage
Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery
2022暑期项目实训(二)
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
std::true_type和std::false_type
std::true_ Type and std:: false_ type
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"