博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oral_quiz->#N个骰子的点数和#
阅读量:5848 次
发布时间:2019-06-19

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

hot3.png

#include 
#include
int g_maxValue = 6;// ====================SolutionI====================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]; for(int i=number; i<=maxSum; ++i) pProbabilities[i-number] = 0; Probability(number, pProbabilities); int total = pow(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); }}void 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, sum+i, pProbabilities);}//=========================SolutionII============================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
=1); ++j) pProbabilities[1-aidFlag][i] += pProbabilities[aidFlag][i-j]; } aidFlag = 1 - aidFlag; } double total = pow((double)g_maxValue, number); for(int i=number; i<=g_maxValue*number; ++i) { double ratio = (double)pProbabilities[aidFlag][i] / total; printf("%d: %e\n", i, ratio); } delete[] pProbabilities[0]; delete[] pProbabilities[1];}// ====================TEST====================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); return 0;}

转载于:https://my.oschina.net/ITHaozi/blog/282818

你可能感兴趣的文章
java中Object转String
查看>>
需求用例分析之备选流
查看>>
redis之如何配置jedisPool参数
查看>>
《系统架构师》——操作系统和硬件基础
查看>>
FTP下载工具
查看>>
oauth2-server-php for windows 的那些坑 (研究中...)
查看>>
文件名后缀详解
查看>>
PHP ElasticSearch的使用
查看>>
微信公众号文章采集方案
查看>>
[转]jquery的一个模板引擎-zt
查看>>
步步为营 .NET 设计模式学习笔记 四、Singleton(单例模式)
查看>>
步步为营 .NET 代码重构学习笔记系列总结
查看>>
最近的几篇NLog相关文章 & 《The Zen of CSS Design》
查看>>
android.app.Activity
查看>>
定时任务quartz
查看>>
Net内存程序集通用脱壳机实现原理(二、反射以及重建方法头)
查看>>
SGU 246 Black & White(思考的转化)
查看>>
HDU 4390 Number Sequence(容斥原理+组合计数)
查看>>
BZOJ 2111 Perm 排列计数(满二叉树)
查看>>
关闭MongoDB
查看>>