当前位置:网站首页>[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;
}
};
边栏推荐
- std::true_ Type and std:: false_ type
- Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
- Easy to use PDF to SVG program
- 华为0基金会——图片整理
- [Android] kotlin code writing standardization document
- 78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
- TCP packet sticking problem
- Will openeuler last long
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- C语言高校实验室预约登记系统
猜你喜欢

关于这次通信故障,我想多说几句…

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

Alibaba cloud international ECS cannot log in to the pagoda panel console

趣-关于undefined的问题
![[Android] kotlin code writing standardization document](/img/d5/53d6a75e87af15799bf7e5d6eb92a5.png)
[Android] kotlin code writing standardization document

Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design

2019 Alibaba cluster dataset Usage Summary

【.NET CORE】 请求长度过长报错解决方案

FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
随机推荐
2022暑期项目实训(二)
Windows connects redis installed on Linux
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
Four processes of program operation
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
Why does wechat use SQLite to save chat records?
Cocos2d Lua 越来越小样本 内存游戏
小程序在产业互联网中的作用
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
2022 Summer Project Training (II)
30 minutes to understand PCA principal component analysis
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
Interview shock 62: what are the precautions for group by?
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
F200 - UAV equipped with domestic open source flight control system based on Model Design
Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
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
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例