为数学考试编写了一小段编程?
我明天要参加考试,考试中有一些关于概率的内容。现在,对于其中许多任务来说,其中之一就是找到所有结果。有时很难做到这一点。假设有两个玩家玩石头剪刀布。一名球员是约翰,另一名球员是里斯。两人连续打了三场比赛。这组三轮比赛的所有可能结果都可以用JTR表示。那将是约翰赢得第一轮,然后平局,然后里斯赢得一轮。我知道有3*3*3=27种不同的结果。我被允许在考试中使用我的电脑,并且有一小段代码可以给出任何字母,在这种情况下至少写出所有 27 种组合将非常好!我只问是否有某种已知的算法可以做到这一点,从此刻起我将亲自破解它。任何建议将非常感激!
I'm taking an exam tomorrow, and on that exam there is a bit about probability. Now on many of those tasks one is to find all the outcomes. That can sometimes be tricky to get right. Let's say you have two players playing rock-paper-scissors. One player is John, the other is Reese. The two play three games in a row. All the possible outcomes of this set with three rounds can be represented as JTR. That would be John wins first round, then there is a tie and then Reese wins a round. I know that there is 3*3*3=27 different outcomes. I am allowed to use my computer on the exam, and a short snippet of code that would given any letters, in this case at least write out all the 27 combinations would be really nice! I only ask in case there is some known algorithm that does this, I'll be hacking away at it myself as of this moment. Any suggestions will be really much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是此类过程的伪代码:
在实际的编程语言中,您可以将集合 {J,T,R} 表示为数字 1、2、3(例如)。
当然,只有当您事先知道轮数时,此片段才有效。对于可变的轮数,您可以使用递归。
Here's the pseudo-code for such a procedure:
In an actual programming language, you could represent the set {J,T,R} as the numbers 1, 2, 3 (for example).
Of course, this snippet would only work if you knew the number of rounds before-hand. For a variable number of rounds, you would use recursion.
尝试以下函数:
像这样调用它:
第一个参数是要排列的项目的数组,第二个参数是一个空数组,只要您想要排列即可,第三个参数应该是 0 到把它踢掉。
Try the following function:
Call it like this:
The first argument is an array of the items you want to permute, the second argument is an empty array that's as long as you want the permutations to be, and the third argument should be 0 to kick it off.