当前位置:网站首页>[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;
}
};
边栏推荐
- atcoder它A Mountaineer
- 推荐好用的后台管理脚手架,人人开源
- Dichotomy (integer dichotomy, real dichotomy)
- DNS hijacking
- 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
- Cocos2d Lua 越来越小样本 内存游戏
- 2022 Summer Project Training (I)
- Interesting - questions about undefined
- Excel usage record
- UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"
猜你喜欢
传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复
Declval (example of return value of guidance function)
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
[swoole series 2.1] run the swoole first
[Android] kotlin code writing standardization document
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
【.NET CORE】 请求长度过长报错解决方案
UDP协议:因性善而简单,难免碰到“城会玩”
Implementation of queue
面向程序员的精品开源字体
随机推荐
Redis的五种数据结构
Coco2017 dataset usage (brief introduction)
node の SQLite
2022 Summer Project Training (II)
AFNetworking框架_上传文件或图像server
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
std::true_type和std::false_type
转载:基于深度学习的工业品组件缺陷检测技术
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
高精度运算
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Interesting - questions about undefined
Alibaba cloud international ECS cannot log in to the pagoda panel console
Common - magic number 7
關於這次通信故障,我想多說幾句…
Principle and usage of extern
阿里云国际版ECS云服务器无法登录宝塔面板控制台
[Android] kotlin code writing standardization document
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
面向程序员的精品开源字体