请设计一个程序,可以按用户输入模拟掷骰子的次数,然后列出每一个数字出现的百分比!
初学学生党求助,班上同学没一个会做的。。。
class Solution:
# @param {int} n an integer
# @return {tuple[]} a list of tuple(sum, probability)
def dicesSum(self, n):
# Write your code here
if n == 0: return None
result = [
[1, 1, 1, 1, 1, 1],
]
for i in range(1, n):
x = 5 * (i + 1) + 1
result.append([0 for _ in range(x)])
for j in range(x):
if j < 6:
result[i][j] = (sum(result[i - 1][0:j + 1]))
elif 6 <= j <= 3 * i + 2:
result[i][j] = (sum(result[i - 1][j - 5:j + 1]))
else:
break
left = 0
right = len(result[i]) - 1
while left <= right:
result[i][right] = result[i][left]
left += 1
right -= 1
res = result[-1]
all = float(sum(res))
other = []
for i, item in enumerate(res):
pro = item / all
other.append([n + i, pro])
return other