蒙蒂霍尔问题_常见的逻辑难题–骑士和刀,蒙蒂·霍尔和就餐哲学家的问题解释...

蒙蒂霍尔问题

While not strictly related to programming, logic puzzles are a good warm up to your next coding session. You may encounter a logic puzzle in your next technical interview as a way to judge your problem solving skills, so it's worth being prepared.

虽然与编程不严格相关,但逻辑难题是您下一次编码会议的良好准备。 在下一次技术面试中,您可能会遇到逻辑难题,以此来判断您的问题解决能力,因此值得做好准备。

In this article, we've collected a few famous logic puzzles and their solutions. Can you solve them without peeking at the answer?

在本文中,我们收集了一些著名的逻辑难题及其解决方案。 您能不偷看答案就能解决它们吗?

骑士和小刀 (Knights and Knaves)

For this logic puzzle, imagine there are two types of people, knights and knaves. Knights only tell the truth, while Knaves only tell lies.

对于这个逻辑难题,想象一下有两种人,骑士和小刀。 骑士只说实话,而刀只讲谎言。

There are many variations of this puzzle, but most involve asking a question to figure out who is the knight and who is the knave.

这个难题有很多变体,但大多数都涉及问一个问题以弄清楚谁是骑士和谁是刀匠。

红色和蓝色 (Red and Blue)

There are two people standing in front of you, Red and Blue. Blue says, "We are both knaves." Who is really the knight and who is the knave?

有两个人站在你面前,红色和蓝色。 蓝说:“我们都是刀子。” 谁是真正的骑士,谁是最专业的人?

SolutionIt's impossible for Blue to be the knight. If Blue was a knight, the statement, "We are both knaves," would actually be a lie. Therefore, Blue is a knave as his statement is a lie, and Red must be a knight.

解决方案 Blue不可能成为骑士。 如果布鲁是一个骑士,那么“我们都是刀”这样的说法实际上就是谎言。 因此,蓝色是个k夫,因为他的陈述是谎言,而红色必须是个骑士。

两条路 (Two Paths)

You arrive at a fork in the road and need to choose the correct path that leads to your destination. There are two people standing at the fork, and you know that one must be a knight and the other must be a knave.

您到达道路上的岔路口,需要选择通往目的地的正确路径。 有两个人站在叉子上,您知道一个人必须是骑士,另一个人必须是a夫。

What single question could you ask to one of the people to determine the correct path, A or B?

您可以问一个人哪个问题来确定正确的路径A或B?

SolutionThe question you can ask either person is, "What path would the other person tell me is the correct one?" The answer will always be the wrong path to take, and you can safely take the other path.Imagine the correct path is A. If you ask directly, "Which is the correct path?" the knave will say B is correct while the knight will say A.However, when asked which path the other person would say is correct, the knave will lie and say that the knight would tell you path B is correct. Meanwhile, the knight will tell the truth about the knave's answer, and say that the knave will tell you that path B is the correct one.In both cases  you know that then answer, path B, is actually a lie, so you should take the other path.

解决方案您可以问一个人的问题是:“另一个人告诉我的是正确的道路?” 答案将永远是错误的路径,您可以放心地选择其他路径。想象一下正确的路径是A。如果您直接问“哪个是正确的路径?”。 刀会说B是正确的,而骑士会说A是正确的。但是,当被问到对方说哪条路径正确时,刀会撒谎并说骑士会告诉您路径B是正确的。 同时,骑士会说出关于刀法的答案的真相,并说刀法会告诉您路径B是正确的答案。在两种情况下,您都知道路径B实际上是一个谎言,因此您应该采取另一条路。

蒙蒂·霍尔问题 (The Monty Hall Problem)

The Monty Hall Problem is a riddle on probability named after the host of the 70’s game show it’s based on, Let’s Make a Deal. This particular problem is a veridical paradox, which means that there is a solution that seems counter-intuitive, yet proven to be true.

蒙蒂·霍尔问题(Monty Hall Problem)是关于概率的一个谜,该概率是根据70年代的游戏节目主持人“ 让我们达成交易”而命名的。 这个特殊的问题是一个垂直悖论 ,这意味着有一种解决方案似乎违反直觉,但却被证明是正确的。

Imagine you are on a game show and there are 3 doors, each with a different prize behind them. Behind one of the three doors is a car. Behind the other two doors there are goats.

假设您正在一场游戏节目中,有3个门,每个门后面都有不同的奖品。 三扇门之一的后面是一辆汽车。 在其他两个门的后面是山羊。

You must choose one of the 3 doors to select as your prize.

您必须选择3个门之一来选择您的奖品。

Say you decide to open Door 1. The host, who knows where the car is, opens a different door, Door 2, which reveals a goat. He then asks if you would like to open Door 3 instead.

假设您决定打开1号门。知道车子在哪里的主人打开了另一扇门2,上面有只山羊。 然后,他询问您是否要打开3号门。

Should you choose Door 3 over your original choice? Does it even matter?

您是否应该选择3号门而不是原来的选择? 有关系吗

SolutionIt turns out that your choice really does matter, and it is actually to your benefit to choose Door 3 instead of Door 1. Here's why.

解决方案事实证明,您的选择确实很重要,选择3号门而不是1号门实际上对您有利。这就是原因。

When you chose Door 1 from the 3 closed doors, you had a 1 out of 3 chance that you picked the right one. Both Door 2 and Door 3 also have a 1 out of the 3 chance of having a car behind it.

当您从3个关闭的门中选择1号门时,您有3个机会中有1个选择了正确的机会。 2号门和3号门在后面都有一辆汽车的可能性也为3的1。

Another way to think about it is that Doors 2 and 3 have a 2 out of 3 chance of having a car behind it combined.

另一种思考的方式是2号门和3号门有3分之二的机会将汽车组合在一起

But when the host opens Door 2 and reveals the goat, you suddenly have more information about the problem.

但是,当主持人打开2号门并露出山羊时,您突然有了有关该问题的更多信息。

Remember that Doors 2 and 3 have a combined probability hiding the car 2/3rds of the time. When Door 2 was opened you know that there was no car behind it.

请记住,第2和第3门的组合概率会将汽车隐瞒2/3次。 当2号门打开时,您会知道后面没有汽车。

But this reveal does not change the combined probability of the two doors. That’s the key takeaway here!

但这并没有改变两个门的组合概率。 这就是这里的重点!

Since you know that Door 2 has a 0/3 chance of hiding the car, you can now say that there's a 2/3 chance that the car is behind Door 3. Door 1 remains unchanged – there's only a 1/3 the car is behind it.

由于您知道2号门藏车的几率是0/3,因此现在您可以说3号门后面的车有2/3的几率。1号门保持不变–只有1/3的车是在它后面。

So if you decide to switch, you go from roughly a 33.33% chance to a 66.67% chance of finding the car. In other words, you are doubling your chances of success by opening Door 3 instead!

因此,如果您决定换车,那您从找到汽车的机会大约为33.33%到66.67%。 换句话说,通过打开3号门,您的成功机会翻倍!

Yes, it is possible that Door 1 was hiding all along and host tricked you. That doesn’t matter. You are gambling by taking the deal, but you’re gambling smart. You should make the best decision with the information you’re given and let the dice roll.

是的,很可能1号门一直隐藏着,主人欺骗了您。 没关系 您通过交易进行赌博,但您在赌博。 您应该根据所获得的信息做出最佳决定,然后掷骰子。

In the long run, you'd perform better by switching than a contestant who decides to go with their first choice. Though it's not immediately obvious, the host is actually doing you a favor by offering you a better deal.

从长远来看,与决定选择他们的第一选择的参赛者相比,通过切换来表现会更好。 尽管目前尚不清楚,但房东实际上通过为您提供更好的协议而帮您一个忙。

餐饮哲学家的问题 (The Dining Philosophers Problem)

The dining philosophers problem is a classic example in computer science to illustrate issues with synchronization. It was originally created by Edsger Dijkstra in 1965, who presented it to his students as a handful of computers competing for access to shared tape drives.

餐饮哲学家问题是计算机科学中的一个经典示例,用于说明同步问题。 它最初是由Edsger Dijkstra于1965年创建的,他将它作为少数几台竞争共享磁带驱动器访问权限的计算机呈现给他的学生。

Imagine five silent philosophers sitting around a table, each with a bowl of spaghetti. There are forks on the table between each pair of adjacent philosophers.

想象一下,有五个沉默的哲学家围坐在一张桌子周围,每个哲学家都拿着一碗意大利面。 每对相邻的哲学家之间的桌子上都有叉子。

Each philosopher can only do one thing at a time: think and eat. However, a philosopher can only eat spaghetti when they have both the left and right forks. A fork can only be held by one philosopher at a time.

每个哲学家一次只能做一件事:思考和吃饭。 但是,只有在拥有左右叉的情况下,哲学家才能吃意大利面。 叉子一次只能由一位哲学家握住。

After a philosopher finishes eating, they need to put down both the left and right forks so they're available to the others. A philosopher can take a fork as soon as it's available, but can only start eating once they have both forks.

哲学家吃完饭后,他们需要放下左右叉,以便其他人都可以使用。 哲学家可以在有叉子的情况下尽快拿起叉子,但只有在拥有两个叉子后才能开始进食。

The philosophers are famous for their appetites – they can all eat endlessly and never get full. On top of that, the bowls of spaghetti magically replenish no matter how much is eaten.

哲学家以食欲而著称-他们都可以无休止地进食,并且永远不会饱食。 最重要的是,无论吃多少意大利面,都能神奇地补充面条。

The problem is, how can can you ensure that no philosopher will starve, and that they can continue eating and thinking forever?

问题是,您如何才能确保没有哲学家会饿死,并且他们可以永远继续进食和思考?

同步与死锁 (Synchronization and Deadlock)

In simple terms, the dining philosophers problem is an illustration of how synchronized access to a shared resource can result in creation of a deadlock situation.

简单来说,餐饮哲学家的问题说明了对共享资源的同步访问如何导致死锁情况的产生。

Synchronization is used to control concurrent access to a shared resource. This is necessary in any situation where multiple independent actors may be competing for the use of one resource like the forks. Since there is only one resource available, we use synchronization to prevent confusion and chaos.

同步用于控制对共享资源的并发访问。 这在多个独立参与者可能争用叉子等一种资源的任何情况下都是必要的。 由于只有一种资源可用,因此我们使用同步来防止混乱和混乱。

A Deadlock is a system state where no progress is possible. This situation can occur when synchronization is enforced, and many processes end up waiting for a shared resource which is being held by some other process. Like with the philosophers who are either stuck eating or thinking, the processes just keep waiting and execute no further.

死锁是系统状态,无法进行进度。 当强制执行同步时,可能会发生这种情况,并且许多进程最终都在等待某个其他进程所持有的共享资源。 就像那些固守饮食或思考的哲学家一样,这些过程只是不断地等待,不再执行。

解决方案 (Solutions)

At first glance it appears like it would not be possible for a deadlock where all philosophers are stuck either eating or thinking. For example, pattern for each philosopher to follow might be:

乍一看,似乎不可能发生所有哲学家都陷入困境的僵局。 例如,每个哲学家可以遵循的模式可能是:

1: think until the left fork is available; when it is, pick it up;

1:思考,直到左叉可用; 如果是的话,拿起它;

1: think until the left fork is available; when it is, pick it up;

1:思考直到左叉可用; 如果是的话,拿起它;

2: think until the right fork is available; when it is, pick it up;

2:思考,直到合适的叉子可用; 如果是的话,拿起它;

2: think until the right fork is available; when it is, pick it up;

2:思考,直到合适的叉子可用; 如果是的话,拿起它;

3: when both forks are held, eat for a fixed amount of time;

3:握住两个叉子时,请进食固定的时间;

3: when both forks are held, eat for a fixed amount of time;

3:握住两个叉子时,请进食固定的时间;

4: then, put the right fork down;

4:然后放下右叉;

4: then, put the right fork down;

4:然后放下右叉;

5: then, put the left fork down;

5:然后放下左叉

5: then, put the left fork down;

5:然后放下左叉

6: repeat from the beginning.

6:从头开始重复

6: repeat from the beginning.

6:从头开始重复

Source: Wikipedia

资料来源: 维基百科

There are many solutions possible to prevent deadlock. If we look closely, one problem in the algorithm above is that all philosophers have equal chance (have the same priority) of acquiring one fork. This prevents anyone from acquiring two forks and the whole system grinds to a halt.

有许多解决方案可以防止死锁。 如果我们仔细观察,上述算法中的一个问题是,所有哲学家都有相同的机会(具有相同的优先级)获得一把叉子。 这样可以防止任何人获得两个叉子,并且整个系统陷入停顿。

Here are some possible solutions:

以下是一些可能的解决方案:

  1. Priority: Some philosophers are assigned higher priority, so that the chance of acquiring both forks is increased.

    优先级 :一些哲学家被赋予更高的优先级,因此增加了获得两把叉子的机会。

  2. Preemption (Politeness): Philosophers relinquish the acquired fork without eating, in case the other fork is not available.

    抢占 (礼貌):如果没有其他叉子,则哲学家不吃东西就放弃获得的叉子。

  3. Arbitration: A mediator allocates forks ensuring that two forks are given to one person, instead of one to many.

    仲裁 :调解员分配分叉,确保将两个分叉分配给一个人,而不是一对多。

Now that you know how to solve these logic puzzles, treat yourself to an endless bowl of spaghetti. You earned it.

既然您知道如何解决这些逻辑难题,那就请自己吃一碗无尽的意大利面。 你赚了

翻译自: https://www.freecodecamp.org/news/common-logic-puzzles-the-knights-and-knaves-monty-hall-and-dining-philosophers-problems-explained/

蒙蒂霍尔问题

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

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

相关文章

西格尔零点猜想_我从埃里克·西格尔学到的东西

西格尔零点猜想I finished reading Eric Siegel’s Predictive Analytics. And I have to say it was an awesome read. How do I define an awesome or great book? A book that changes your attitude permanently. You must not be the same person that you were before y…

C/C++实现删除字符串的首尾空格

StdStringTrimTest.cpp #include <iostream> int main() {std::string str(" 字符串 String ");std::cout << str << std::endl;std::cout << str.size() << std::endl;str.erase(str.find_first_of( ), str.find_first_not_of…

assign复制对象_JavaScript标准对象:assign,values,hasOwnProperty和getOwnPropertyNames方法介绍...

assign复制对象In JavaScript, the Object data type is used to store key value pairs, and like the Array data type, contain many useful methods. These are some useful methods youll use while working with objects.在JavaScript中&#xff0c; Object数据类型用于存…

HDFS 技术

HDFS定义 Hadoop Distributed File System&#xff0c;是一个使用 Java 实现的、分布式的、可横向扩展的文件系 统&#xff0c;是 HADOOP 的核心组件 HDFS特点 处理超大文件流式地访问数据运行于廉价的商用机器集群上&#xff1b; HDFS 不适合以下场合&#xff1a;低延迟数据…

深度学习算法和机器学习算法_啊哈! 4种流行的机器学习算法的片刻

深度学习算法和机器学习算法Most people are either in two camps:大多数人都在两个营地中&#xff1a; I don’t understand these machine learning algorithms. 我不了解这些机器学习算法。 I understand how the algorithms work, but not why they work. 我理解的算法是如…

Python第一次周考(0402)

2019独角兽企业重金招聘Python工程师标准>>> 一、单选 1、Python3中下列语句错误的有哪些&#xff1f; A s input() B s raw_input() C print(hello world.) D print(hello world.) 2、下面哪个是 Pycharm 在 Windows 下 默认 用于“批量注释”的快捷键 A Ctrl d…

express 路由中间件_Express通过示例进行解释-安装,路由,中间件等

express 路由中间件表达 (Express) When it comes to build web applications using Node.js, creating a server can take a lot of time. Over the years Node.js has matured enough due to the support from community. Using Node.js as a backend for web applications a…

ASP.NET 页面之间传值的几种方式

对于任何一个初学者来说&#xff0c;页面之间传值可谓是必经之路&#xff0c;却又是他们的难点。其实&#xff0c;对大部分高手来说&#xff0c;未必不是难点。 回想2016年面试的将近300人中&#xff0c;有实习生&#xff0c;有应届毕业生&#xff0c;有1-3年经验的&#xff0c…

Mapreduce原理和YARN

MapReduce定义 MapReduce是一种分布式计算框架&#xff0c;由Google公司2004年首次提出&#xff0c;并贡献给Apache基金会。 MR版本 MapReduce 1.0&#xff0c;Hadoop早期版本(只支持MR模型)MapReduce 2.0&#xff0c;Hadoop 2.X版本&#xff08;引入了YARN资源调度框架后&a…

数据可视化图表类型_数据可视化中12种最常见的图表类型

数据可视化图表类型In the current era of large amounts of information in the form of numbers available everywhere, it is a difficult task to understand and get insights from these dense piles of data.在当今时代&#xff0c;到处都是数字形式的大量信息&#xff…

三大纪律七项注意(Access数据库)

三大纪律&#xff08;规则或范式&#xff09; 要有主键其他字段依赖主键其他字段之间不能依赖七项注意 一表一主键(订单表&#xff1a;订单号&#xff1b;订单明细表&#xff1a;订单号产品编号)经常查&#xff0c;建索引&#xff0c;小数据&#xff08;日期&#xff0c;数字类…

CentOS下安装JDK的三种方法

来源&#xff1a;Linux社区 作者&#xff1a;spiders http://www.linuxidc.com/Linux/2016-09/134941.htm 由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一&#xff1a;手动解压JDK的压缩包&#xff0c;然后…

MapReduce编程

自定义Mapper类 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> &#xff5b; … }自定义mapper类都必须实现Mapper类&#xff0c;有4个类型参数&#xff0c;分别是&#xff1a; Object&#xff1a;Input Key Type-------------K1Text: Input…

统计信息在数据库中的作用_统计在行业中的作用

统计信息在数据库中的作用数据科学与机器学习 (DATA SCIENCE AND MACHINE LEARNING) Statistics are everywhere, and most industries rely on statistics and statistical thinking to support their business. The interest to grasp on statistics also required to become…

IOS手机关于音乐自动播放问题的解决办法

2019独角兽企业重金招聘Python工程师标准>>> 评估手机自带浏览器不能识别 aduio标签重的autoplay属性 也不能自动执行play()方法 一个有效的解决方案是在微信jssdk中调用play方法 document.addEventListener("WeixinJSBridgeReady", function () { docum…

svg标签和svg文件区别_什么是SVG文件? SVG图片和标签说明

svg标签和svg文件区别SVG (SVG) SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded…

开发人员怎么看实施人员

英文原文&#xff1a;What Developers Think Of Operations&#xff0c;翻译&#xff1a;张红月CSDN 在一个公司里面&#xff0c;开发和产品实施对于IS/IT的使用是至关重要的&#xff0c;一个负责产品的研发工作&#xff0c;另外一个负责产品的安装、调试等工作。但是在开发人员…

怎么评价两组数据是否接近_接近组数据(组间)

怎么评价两组数据是否接近接近组数据(组间) (Approaching group data (between-group)) A typical situation regarding solving an experimental question using a data-driven approach involves several groups that differ in (hopefully) one, sometimes more variables.使…

代码审计之DocCms漏洞分析

0x01 前言 DocCms[音译&#xff1a;稻壳Cms] &#xff0c;定位于为企业、站长、开发者、网络公司、VI策划设计公司、SEO推广营销公司、网站初学者等用户 量身打造的一款全新企业建站、内容管理系统&#xff0c;服务于企业品牌信息化建设&#xff0c;也适应用个人、门户网站建设…

你让,勋爵? 使用Jenkins声明性管道的Docker中的Docker

Resources. When they are unlimited they are not important. But when theyre limited, boy do you have challenges! 资源。 当它们不受限制时&#xff0c;它们并不重要。 但是&#xff0c;当他们受到限制时&#xff0c;男孩你有挑战&#xff01; Recently, my team has fa…