Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the points of n dice by LeetCode

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces LeetCode how to find n dice points, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Topic description

Throw n dice on the ground and the sum of the points on one side of all the dice is s. Enter n to print out the probability of occurrence of all possible values of s.

You need to return the answer with a floating-point array, where the I element represents the probability of the smaller I of the set of points that the n dice can throw.

1 probability dictionary. Initialize dp [0] = 1. The probability that the sum of points is 0 for 0 dice is 1.

# after adding a dice, we only need to add 1x 6 to the sum of each point in the original dictionary as the sum of the new points, and multiply the original probability by 1 stroke 6 to add to the new points and the corresponding probability

Dp = {}

Dp [0] = 1

For i in range (1, n + 1):

Newdp = collections.defaultdict (int)

For sm in dp:

For v in range (1,7):

# after adding a dice, add its probability to the new point sum

Newdp [sm + v] + = dp [sm] / 6

Dp = newdp

Res = []

For sm in range (n, 6 * n + 1):

# store the values in the result in turn

Res.append (dp[ sm])

Return res

Thank you for reading this article carefully. I hope the article "how to get the points of n dice" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 232

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report