【剑指offer】题43:n个骰子的点数
来源:互联网 发布:淘宝标题促销词 编辑:程序博客网 时间:2024/06/01 07:34
动态规划
使用dp[k][i]表示k个骰子时,点数和为i的组合数
则转移状态方程:
void func(int n){ if (n < 1) { return; } vector<int> vec(n*6+1); vector<vector<int>> dp(n+1,vec); for (auto i = 1; i <= 6;i++) { dp[1][i] = 1; } for (auto k = 1; k < n; ++k) { for (auto i = k; i < 6*k+1; ++i) { for (auto j = 1; j <= 6; j++) { dp[k + 1][i + j] += dp[k][i]; } } } int sum = accumulate(dp[n].begin(), dp[n].end(),0); for (auto i = 0; i < 6 * n + 1;++i) { if (dp[n][i]!=0) { cout << i << " " << (double)dp[n][i] / (double)sum<<endl; } }}
阅读全文
0 0
- 【剑指offer】题43:n个骰子的点数
- 剑指offer面试题43 n个骰子的点数
- 【剑指offer】面试题43:n个骰子的点数
- 剑指Offer:面试题43 n个骰子的点数
- 剑指offer 43-n个骰子的点数
- 面试题43:n个骰子的点数(《剑指offer》)
- 剑指offer-面试题43:n个骰子的点数
- 【剑指offer-Java版】43n个骰子的点数
- 《剑指offer》:[43]N个骰子的点数
- 剑指offer面试题43:n个骰子的点数
- 《剑指offer》43:n个骰子的点数
- 剑指offer-面试题43-n个骰子的点数
- 剑指offer 面试题43 n个骰子的点数
- 剑指Offer之 - n个骰子的点数
- 【剑指offer系列】 n个骰子的点数___43
- 剑指offer(47):n个骰子的点数
- 剑指offer:n个骰子的点数(java)
- 剑指offer 43. n个骰子的点数
- UIWebView加载网页内存占用过高
- iOS开发NSString篇:字符串每隔几个字符插入某个字符
- curl验证,调用webservice接口
- 夜神模拟器和hbuilder连接
- Java将数据写入word文档(.doc)
- 【剑指offer】题43:n个骰子的点数
- mui轮播图片
- java连接hive
- cocopods 异常处理
- Android 生命周期学习
- svn如何提交so文件
- 听过很多道理,却依然过不好这一生
- ELF文件格式解析
- Unity播放gif