为数学考试编写了一小段编程?

发布于 2024-12-17 11:42:18 字数 275 浏览 0 评论 0原文

我明天要参加考试,考试中有一些关于概率的内容。现在,对于其中许多任务来说,其中之一就是找到所有结果。有时很难做到这一点。假设有两个玩家玩石头剪刀布。一名球员是约翰,另一名球员是里斯。两人连续打了三场比赛。这组三轮比赛的所有可能结果都可以用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-12-24 11:42:18

以下是此类过程的伪代码:

proc rps()
    for outcome1 in {J,T,R}
        for outcome2 in {J,T,R}
            for outcome3 in {J,T,R}
                print (outcome1, outcome2, outcome3)

在实际的编程语言中,您可以将集合 {J,T,R} 表示为数字 1、2、3(例如)。

当然,只有当您事先知道轮数时,此片段才有效。对于可变的轮数,您可以使用递归。

Here's the pseudo-code for such a procedure:

proc rps()
    for outcome1 in {J,T,R}
        for outcome2 in {J,T,R}
            for outcome3 in {J,T,R}
                print (outcome1, outcome2, outcome3)

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.

阿楠 2024-12-24 11:42:18

尝试以下函数:

void permute(String[] items, String[] soFar, int depth) {
    if (depth == soFar.length) {
        System.out.println(Arrays.toString(soFar));
        return;
    }
    for(String item: items) {
        soFar[depth] = item;
            permute(items,soFar,depth+1);
    }
}

像这样调用它:

permute(new String[] {"J","T","R"},new String[3],0);

第一个参数是要排列的项目的数组,第二个参数是一个空数组,只要您想要排列即可,第三个参数应该是 0 到把它踢掉。

Try the following function:

void permute(String[] items, String[] soFar, int depth) {
    if (depth == soFar.length) {
        System.out.println(Arrays.toString(soFar));
        return;
    }
    for(String item: items) {
        soFar[depth] = item;
            permute(items,soFar,depth+1);
    }
}

Call it like this:

permute(new String[] {"J","T","R"},new String[3],0);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文