// 面试题60:n个骰子的点数 // 题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s // 的所有可能的值出现的概率。 #include <cstdio> #include <math.h> int g_maxValue = 6; // ====================方法一==================== void Probability(int current, int sum, int* pProbabilities); void PrintProbability_Solution1(int number) { if (number < 1) return; int maxSum = number * g_maxValue; //n个骰子的最大点数 //注意此时 + 1是因为数组从0计数 int* pProbabilities = new int[maxSum + 1]; //存放点数和s的出现次数 for (int i = number; i <= maxSum; ++i) //为了直观,数组的[0~number-1]没有用 pProbabilities[i] = 0; for (int i = 1; i <= g_maxValue; ++i) //第一个骰子, i指该骰子的点数 Probability(number, i, pProbabilities); int total = pow((double)g_maxValue, number); //点数所有可能总数 for (int i = number; i <= maxSum; ++i) { double ratio = (double)pProbabilities[i] / total; printf("%d: %e ", i, ratio); } delete[] pProbabilities; } void Probability(int current, int sum, int* pProbabilities) //更新current个骰子点数和 { if (current == 1) pProbabilities[sum]++; else { //f(n,s) = f(n-1,s-1) + f(n-1,s-2) + f(n-1,s-3) // + f(n-1,s-4) + f(n-1,s-5) + f(n-1,s-6) for (int i = 1; i <= g_maxValue; ++i) Probability(current - 1, i + sum, pProbabilities); //通过i+sum传递当前点数和 } } // ====================方法二==================== // 原理看懂了, 实现没咋看懂 void PrintProbability_Solution2(int number) { if (number < 1) return; int* pProbabilities[2]; pProbabilities[0] = new int[g_maxValue * number + 1]; pProbabilities[1] = new int[g_maxValue * number + 1]; for (int i = 0; i < g_maxValue * number + 1; ++i) { pProbabilities[0][i] = 0; pProbabilities[1][i] = 0; } int flag = 0; for (int i = 1; i <= g_maxValue; ++i) //第一个骰子, 每个值只出现一次, 第n个数字表示和为n出现的次数 pProbabilities[flag][i] = 1; for (int k = 2; k <= number; ++k) //从第二个骰子开始 { for (int i = 0; i < k; ++i) //清空另一个数组的前k项, 以前的统计结果 pProbabilities[1 - flag][i] = 0; for (int i = k; i <= g_maxValue * k; ++i) { pProbabilities[1 - flag][i] = 0; for (int j = 1; j <= i && j <= g_maxValue; ++j) pProbabilities[1 - flag][i] += pProbabilities[flag][i - j]; //前一个数组 n-1 ~ n-6之和 } flag = 1 - flag; //交换数组 } double total = pow((double)g_maxValue, number); for (int i = number; i <= g_maxValue * number; ++i) { double ratio = (double)pProbabilities[flag][i] / total; printf("%d: %e ", i, ratio); } delete[] pProbabilities[0]; delete[] pProbabilities[1]; }
// ====================测试代码==================== void Test(int n) { printf("Test for %d begins: ", n); printf("Test for solution1 "); PrintProbability_Solution1(n); printf("Test for solution2 "); PrintProbability_Solution2(n); printf(" "); } int main(int argc, char* argv[]) { Test(1); Test(2); Test(3); Test(4); //Test(11); Test(0); return 0; }
分析:第一个解法实质上是动态规划,第二个解法看不懂实现过程。
动态规划:https://blog.csdn.net/whuqin/article/details/6639187
动态规划就是分阶段考虑问题,给出变量,找出相邻阶段间的关系。具体定义给忘了。
1.现在变量有:骰子个数,点数和。当有k个骰子,点数和为n时,出现次数记为f(k,n)。那与k-1个骰子阶段之间的关系是怎样的?
2.当我有k-1个骰子时,再增加一个骰子,这个骰子的点数只可能为1、2、3、4、5或6。那k个骰子得到点数和为n的情况有:
(k-1,n-1):第k个骰子投了点数1
(k-1,n-2):第k个骰子投了点数2
(k-1,n-3):第k个骰子投了点数3
....
(k-1,n-6):第k个骰子投了点数6
在k-1个骰子的基础上,再增加一个骰子出现点数和为n的结果只有这6种情况!
所以:f(k,n)=f(k-1,n-1)+f(k-1,n-2)+f(k-1,n-3)+f(k-1,n-4)+f(k-1,n-5)+f(k-1,n-6)
3.有1个骰子,f(1,1)=f(1,2)=f(1,3)=f(1,4)=f(1,5)=f(1,6)=1。
那代码就容易写了,递归函数,返回和为n出现的次数。所有的和出现次数总和为6^n。