题目描述:把 n 个骰子扔在地上,所有骰子朝上一面的点数之和为 s。输入 n,打印出 s 的所有可能的值出现的概率。
import Foundation
class For43Solution {
func diceProbility(diceCount:Int) {
var arr = [[Int]]()
let maxCount = 6 * diceCount + 1
let first = [Int](repeating: 0, count: maxCount)
let next = [Int](repeating: 0, count: maxCount)
arr.append(first)
arr.append(next)
var flag = 0
//一个骰子的情况
for i in 1...6 {
arr[flag][i] = 1
}
//剩下的骰子
for i in 2...diceCount {
//清空不存在的点数,2个骰子不存在1
for j in 0..<i {
arr[1-flag][j] = 0
}
//计算每个存在的点数
for k in i...6*i {
var m = 1
arr[1-flag][k] = 0
while m <= 6 && m <= k {
arr[1-flag][k] += arr[flag][k-m]
m += 1
}
}
flag = 1 - flag
}
//打印
let total :Double = pow(6.0, Double(diceCount))
var index = 0
for i in arr[flag] {
print("点数\(index)出现的概率:\(Double(i)/total)")
index += 1
}
}
}
算法思想:动态规划求解,每次都算出可能存在的所有点数。
github地址:https://github.com/cubegao/LeetCode