funcprintProbability(n int)(result []float64) { var ( i, j, k int total = math.Pow(float64(gmax), float64(n)) flag = 0 probabilities = make([][]int, 2) ) probabilities[0] = make([]int, n*gmax+1) probabilities[1] = make([]int, n*gmax+1)
// 初始掷第一枚骰子 for i = 1; i <= gmax; i++{ probabilities[flag][i] = 1 }
for k = 2; k <= n; k++ { for i = 0; i < k; i++ { probabilities[1-flag][i] = 0 }
for i = k; i <= k*gmax; i++ { // 使用i个骰子最小点数为i,最大点数6*i probabilities[1-flag][i] = 0 for j = 1; i-j >= 0 && j <= gmax; j++ { // 第j个骰子的6种情况 probabilities[1-flag][i] += probabilities[flag][i-j] } } flag = 1 - flag }
result = make([]float64, gmax*n-n+1) for i = n; i <= gmax*n; i++ { result[i-n] = float64(probabilities[flag][i]) / total }
return result }
funcmain() { result := printProbability(3) for i := 0; i < len(result); i++ { fmt.Println(result[i]) } }