博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《剑指offer》第六十题(n个骰子的点数)
阅读量:5021 次
发布时间:2019-06-12

本文共 3179 字,大约阅读时间需要 10 分钟。

// 面试题60:n个骰子的点数// 题目:把n个骰子扔在地上,所有骰子朝上一面的点数之和为s。输入n,打印出s// 的所有可能的值出现的概率。#include 
#include
int g_maxValue = 6;// ====================方法一====================//使用递归,还是会有重复计算void Probability(int number, int* pProbabilities);void Probability(int original, int current, int sum, int* pProbabilities);void PrintProbability_Solution1(int number){ if (number < 1) return; int maxSum = number * g_maxValue; int* pProbabilities = new int[maxSum - number + 1];//建立一个长为maxSum - number + 1的数组,用来统计次数 for (int i = number; i <= maxSum; ++i)//初始化为0 pProbabilities[i - number] = 0; Probability(number, pProbabilities);//统计次数 int total = pow((double)g_maxValue, number); for (int i = number; i <= maxSum; ++i) { double ratio = (double)pProbabilities[i - number] / total; printf("%d: %e\n", i, ratio); } delete[] pProbabilities;}void Probability(int number, int* pProbabilities){ for (int i = 1; i <= g_maxValue; ++i) Probability(number, number, i, pProbabilities);}//划分为1个和n-1个两堆色子,然后迭代如此划分,遍历所有可能,然后pProbabilities数组相应加1void Probability(int original, int current, int sum, int* pProbabilities){ if (current == 1) { pProbabilities[sum - original]++; } else { for (int i = 1; i <= g_maxValue; ++i) { Probability(original, current - 1, i + sum, pProbabilities); } }}// ====================方法二====================//使用循环方法,需要找到统计新的一个色子的规律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)//建立两个数组,初始化为0 { pProbabilities[0][i] = 0; pProbabilities[1][i] = 0; } int flag = 0; for (int i = 1; i <= g_maxValue; ++i) pProbabilities[flag][i] = 1;//第一个色子,每个值出现次数为1,值1~g_maxValue for (int k = 2; k <= number; ++k)//从第二个色子开始统计 { for (int i = 0; i < k; ++i) pProbabilities[1 - flag][i] = 0;//把另一个数组的k之前的次数清零,因为那是以前的统计结果,后面不可能出现的值 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]; } 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\n", i, ratio); } delete[] pProbabilities[0]; delete[] pProbabilities[1];}// ====================测试代码====================void Test(int n){ printf("Test for %d begins:\n", n); printf("Test for solution1\n"); PrintProbability_Solution1(n); printf("Test for solution2\n"); PrintProbability_Solution2(n); printf("\n");}int main(int argc, char* argv[]){ Test(1); Test(2); Test(3); Test(4); Test(11); Test(0); system("pause"); return 0;}

 

转载于:https://www.cnblogs.com/CJT-blog/p/10545897.html

你可能感兴趣的文章
mysql存储过程
查看>>
洛谷P2556 [AHOI2002] 黑白图像压缩 [模拟]
查看>>
letecode [136] - Single Number
查看>>
linux下设置固定IP的方法
查看>>
VMware虚拟机下Linux系统的全屏显示
查看>>
net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>
graphite custom functions
查看>>
列出所有的属性键
查看>>
js获取请求地址后面带的参数
查看>>
[原创]使用java批量修改文件编码(ANSI-->UTF-8)
查看>>