掷骰子

Description:

描述:

In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds.

在本文中,我们将看到一个动态的编程问题,该问题可以在任何采访回合中体现。

Problem statement:

问题陈述:

Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.

给定n个骰子,每个骰子有m个面(从1m编号),请找到获得和X的方法数量。 X是抛出所有骰子时每个面上的值之和。

    Input:
n=3
m=3
X=6
Output:
Total number of ways are: 7

Explanation:

说明:

    Total number of dices: 3 say x1,x2,x3
Number of faces on each dice: 3 (1 to 3)
Total sum to be achieved: 6
We will write as xi(j)which means face value of dice xi is j 
So sum 6 can be achieved in following ways:
6=x1(1)+x2(2)+x3(3)
6=x1(1)+x2(3)+x3(2)
6=x1(2)+x2(2)+x3(2)
6=x1(2)+x2(3)+x3(1)
6=x1(2)+x2(1)+x3(3)
6=x1(3)+x2(2)+x3(3)
6=x1(3)+x2(3)+x3(1)
This are total 7 ways to achieve the sum.

Solution Approach:

解决方法:

If it was only 1 dice, then if X<=m, the answer would be 1 else 0. Since there is only one way to achieve the sum if possible as there is only one dice.

如果只有1个骰子,那么如果X <= m ,答案将是1否则为0。由于只有一种骰子,因此只有一种方法可以实现总和。

Now when n, number of dice>1, then the problem becomes a recursive one

现在,当n ,骰子数> 1时,问题就变成了递归问题

We can think of the recursive function as f(n,X) where n is number of dice and X is desired sum.

我们可以将递归函数视为f(n,X) ,其中n是骰子数, X是期望的和。

A single dice has m choices, which means the face can have values ranging 1 to m
So,

一个骰子有m个选择,这意味着该面Kong的取值范围为1到m
所以,

Recursively we can write,

我们可以递归地写

dice throw formula

That means summation of all choices for this particular dice to have face value 1 to minimum(X, m)

这意味着该特定骰子的所有选择的总和的面值为1到最小值(X,m)

For our example case, n=3, m=3, X=6

对于我们的示例情况, n = 3,m = 3,X = 6

So, we need to find f(3,6)

因此,我们需要找到f(3,6)

    f(3,6)=f(2,5)+f(2,4)+f(2,3)

f(2,5), f(2,4), f(2,3) all are sub problems themselves which are needed to be solved further. This would generate a recursion tree.

f(2,5),f(2,4),f(2,3)本身都是子问题,需要进一步解决。 这将生成一个递归树。

Of course, we have base cases for single dice which is f(1,i)=1 for i=1 to m

当然,我们有单个骰子的基本情况, 对于i = 1到m,f(1,i)= 1

But this recursion will generate many overlapping sub problems, hence, we need to convert it to dynamic programing.

但是这种递归将产生许多重叠的子问题,因此,我们需要将其转换为动态编程。

    1)  Declare dp[n+1][x+1] similar to f(n,x). Initialize it to 0.
2)  Implement the base case f(1,i)
for i=1 to i minimum(m ,x)
dp[1][i]=1;
3)  Fill the other values as per recursion relation
for i=2 to n //iterate for number of dices
for j=1 to x //iterate for sums
for k=1 to minimum(m ,j)
//iterate for face values up to minimum(m,j),j be the subsum
dp[i][j]+=dp[i-1][j-k];
end for
end for
end for    
4)  The answer is dp[n][x]

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
long long int dicethrow(int m, int n, int x) {
if (m * n < x)
return 0;
long long dp[n + 1][x + 1];
memset(dp, 0, sizeof(dp));
//base case
for (int i = 1; i <= m && i <= x; i++)
dp[1][i] = 1;
for (int i = 2; i <= n; i++) { //iterate for number of dices
for (int j = 1; j <= x; j++) { //iterate for sums
//iterate for face values up to minimum(m,j),j be the subsum
for (int k = 1; k <= m & k < j; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
return dp[n][x];
}
int main() {
int n, m, x;
cout << "Enter number of dices, n:\n";
cin >> n;
cout << "Enter number of faces on a dice, m:\n";
cin >> m;
cout << "Enter sum, X:\n";
cin >> x;
cout << "Number of ways to achieve sum: " << dicethrow(m, n, x) << endl;
return 0;
}

Output

输出量

RUN 1:
Enter number of dices, n:
3
Enter number of faces on a dice, m:
3
Enter sum, X:
6
Number of ways to achieve sum: 7
RUN 2:
Enter number of dices, n:
3
Enter number of faces on a dice, m:
3
Enter sum, X:
12
Number of ways to achieve sum: 0

In the second output there is no way to acquire the sum which can be verified as m*n<X. It's better practise to keep such base case to optimize your code :)

在第二个输出中,无法获取可以验证为m * n <X的和 。 最好保留此类基本情况以优化代码:)

翻译自: https://www.includehelp.com/icp/dice-throw.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377005.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《YOLO算法笔记》(草稿)

检测算法回顾 5、6年前的检测算法大体如下&#xff1a; 手动涉及特征时应该考虑的因素&#xff1a; 1、尺度不变性 2、光照不变性 3、旋转不变性 这一步骤称为特征工程&#xff0c;最重要的一个算法称为sift&#xff0c;(回顾SIFT讲解)体现了上述所有的观点。 在分类的过程中…

U盘安装Centos6.3

一 首先下载Centos6.3的光盘镜像文件&#xff0c;网上到镜像实在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下载个新版本的UltraISO, 在其菜单“启动”下有“写入硬盘镜像“功能到&#xff0c;原来用到绿色版本是8.6.2.2011不支持&#xff0c;…

[转]粵語固有辭彙與漢語北方話辭彙對照

本文转自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer调用geoserver发布的地图实现地图的基本功能

转自&#xff1a;http://starting.iteye.com/blog/1039809 主要实现的功能有放大&#xff0c;缩小&#xff0c;获取地图大小&#xff0c;平移&#xff0c;线路测量&#xff0c;面积测量&#xff0c;拉宽功能&#xff0c;显示标注&#xff0c;移除标注&#xff0c;画多边形获取经…

LLVM与Codegen技术

LLVM 百度百科 LLVM是构架编译器(compiler)的框架系统&#xff0c;以C编写而成&#xff0c;用于优化以任意程序语言编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(run-time)以及空闲时间(idle-time)&#xff0c;对开发者保持开放&#xff0c;并兼容已有…

跟乌克兰人学编程1

今天要Disable一个菜单&#xff0c;工程项目多&#xff0c;不容易找。 乌克兰人建议我用Spy&#xff0c;将靶拖到目标窗体上就可以看到类名。转载于:https://www.cnblogs.com/SunWentao/archive/2012/12/19/2825220.html

html网页转图片_HTML图片

html网页转图片HTML图片 (HTML Images) Images are visuals of something that look elegant. In web pages, images are used to create a good and appealing design. 图像是外观精美的视觉效果。 在网页中&#xff0c;图像用于创建良好且吸引人的设计。 The <img> ta…

Android学习拾遗

1. java中的flush()作用&#xff1a;强制将输出流缓冲区的数据送出。 2. 文件存储&#xff1a; 存储到内部&#xff1a;另外使用一个class实现&#xff0c;最开始初始化用了this,后来放在这里不合适&#xff0c;改成了带参数的构造方法。 包括存储、读取、追加 读取&#xff1a…

OLAP 技术之列式存储与数据压缩(快查询方法之一)

前言 列式存储和数据压缩&#xff0c;对于一款高性能数据库来说是必不可少的特性。一个非常流行的观点认为&#xff0c;如果你想让查询变得更快&#xff0c;最简单且有效的方法是减少数据扫描范围和数据传输时的大小&#xff0c;而列式存储和数据压缩就可以帮助我们实现上述两…

sql 视图嵌套视图_SQL视图

sql 视图嵌套视图SQL | 观看次数 (SQL | Views) Views in SQL are virtual tables. A view also has rows and columns as theyre during a real table within the database. We will create a view by selecting fields from one or more tables present within the database.…

Postgresql多线程hashjoin(inner join)

pg hashjoin 节点大致步骤&#xff1a; 1、分块与分桶。对一个表hash时&#xff0c;确定块数和桶数量。&#xff08;一块被划分为10个元组的桶&#xff09;确定分块号与分桶号是由hashvalue决定的。 2、执行&#xff1a; 1、顺序获取S表中所有元组&#xff0c;对每一条元组Has…

iframe实现局部刷新和回调--开篇

今天做项目遇到一个问题。就是提交表单的时候&#xff0c;验证用户名是否存在和验证码是否正确。 当验证码或者用户名存在的时候。在后台弹窗提示。可页面原本file里面符合要求的值刷新没了。用户体验不好。因为用ifream刷新技术已不是什么新鲜技术。所以网上有大把的资料可参考…

Java文件类boolean setExecutable(boolean exec_file,boolean owner_access)方法,带示例

文件类boolean setExecutable(boolean exec_file&#xff0c;boolean owner_access) (File Class boolean setExecutable(boolean exec_file , boolean owner_access)) This method is available in package java.io.File.setExecutable(boolean exec_file , boolean owner_acc…

OLTP 系统和 OLAP 系统的核心设计思想

关于 OLTP 系统和 OLAP 系统的核心设计思想 数据存储系统的关于查询的典型操作&#xff1a; -- 第一种需求&#xff1a; 根据 key&#xff08;1&#xff09; 找 value&#xff08;name,age&#xff09;&#xff0c; 单点查询 select name, age from student where id 1; stu…

虚拟机

vt-x 虚拟技术的硬盘支持。想像成“硬解码”的东东。不是装虚拟机必须的&#xff0c;但有它效果会好些。 vt-x检测工具&#xff1a;securable.exe 下载地址&#xff1a;http://pan.baidu.com/s/1kTBOvzD Hardware Virtualization选项&#xff1a; no [CPU和BIOS都不支持VT] loc…

算法(转)

欢迎自荐和推荐链接。 算法 优秀博客推荐&#xff1a;各种数据结构与算法知识入门经典&#xff08;不断更新)基本算法 贪心算法&#xff1a;贪心算法 作者&#xff1a;独酌逸醉 贪心算法精讲 作者&#xff1a;3522021224 递归和分治&#xff1a;递归与分治策略 …

sjf调度算法_如何通过静态方法预测SJF调度中未来过程的突发时间?

sjf调度算法In SJF Scheduling, CPU is assigned to the process having the smallest burst time but it can not be implemented practically, because we dont know burst time of the arrived processes in advance. 在SJF Scheduling中 &#xff0c;将CPU分配给具有最短突…

flask 知识点总结

request对象的常用属性具体使用方法如下:request.headers, request.headers.get(If-None-Match)request.json, request.json[value] 或 request.json.get(detail_msg, "")request.args, request.args.get(limit, 10)来获取query parametersrequest.form, request.for…

Postgresql中的hybrid hash join(无状态机讲解)

hybrid hash join hybrid hash join是基于grace hash join 的优化。 在postgresql中的grace hash join 是这样做的&#xff1a;inner table太大不能一次性全部放到内存中&#xff0c;pg会把inner table 和outer table按照join的key分成多个分区&#xff0c;每个分区(有一个inn…

末日中的黎明

哈哈&#xff0c; 今天是2012-12-21&#xff0c;传说中的世界末日&#xff0c;不过现在看来&#xff0c;一切都是空的。。。 在这个容易记忆的日子里&#xff0c;我的博客开通了。他将伴随我以后的学习开发&#xff0c;期望我能充分利用博客&#xff0c;帮我养成常总结、常记笔…