当前位置:网站首页>[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;
}
};
边栏推荐
- 30 minutes to understand PCA principal component analysis
- 重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
- atcoder它A Mountaineer
- 解读云原生技术
- Recursive way
- MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
- Recommend easy-to-use backstage management scaffolding, everyone open source
- 第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
- MarkDown语法——更好地写博客
- Cobra 快速入门 - 专为命令行程序而生
猜你喜欢
STM32按键状态机2——状态简化与增加长按功能
Alibaba cloud international ECS cannot log in to the pagoda panel console
虚拟机VirtualBox和Vagrant安装
TOP命令详解
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
I want to say more about this communication failure
Grafana 9.0 is officially released! It's the strongest!
【Android】Kotlin代码编写规范化文档
Virtual machine VirtualBox and vagrant installation
Introduction to the usage of model view delegate principal-agent mechanism in QT
随机推荐
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
2022暑期项目实训(三)
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
Maixll dock camera usage
ADB common commands
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
STM32+HC05串口蓝牙设计简易的蓝牙音箱
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
简单易用的PDF转SVG程序
[Android] kotlin code writing standardization document
Kill -9 system call used by PID to kill process
Grafana 9.0 is officially released! It's the strongest!
atcoder它A Mountaineer
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
High precision operation
STM32+ESP8266+MQTT协议连接OneNet物联网平台
【Swoole系列2.1】先把Swoole跑起来
1700C - Helping the Nature
MSF horizontal MSF port forwarding + routing table +socks5+proxychains