如何在Java中解决哈希碰撞

使用Java编写以下程序。

假设玩家1有7个骰子,玩家2有5个骰子(12个骰子都是标准的1到6的骰子,而且是公平的)。两个玩家都掷出他们的骰子,并比较他们各自的总和(即玩家1掷出1,3,5,2,6,1,1,1=19,玩家2掷出2,1,4,6,3=16)。如果玩家1掷出的总和高于玩家2,则玩家1获胜,否则玩家2获胜。如果所有2,176,782,336个组合都掷出,玩家1将赢得多少场比赛?有多少场比赛将由闲家2获胜?有多少场比赛会出现平局?(注意:只有回答这三个问题的总数才需要打印)

我被卡住的部分是如何保证没有重复的卷子?

谢谢你

import java.util.Random;

public class KDice {
public static void main(String[] args) {
    Random random = new Random();
    long playerOneWins = 0, playerTwoWins = 0, ties = 0;
    int playerOneSum, playerTwoSum;

    //for long number use L as suffix
    for (long i = 0; i < 2176782336L; i++) {
    //roll dice for player 1
        playerOneSum = rollDice(random, 7);
    //roll dice for player 2
        playerTwoSum = rollDice(random, 5);
        //find who won
        if (playerOneSum == playerTwoSum) {
            ties++;
        } else if (playerOneSum > playerTwoSum) {
            playerOneWins++;
        } else {
            playerTwoWins++;
        }
    }
    //after all the round done, display stats
    System.out.println("Player 1 win: " + playerOneWins);
    System.out.println("Player 2 win: " + playerTwoWins);
    System.out.println("Ties: " + ties);
}

public static int rollDice(Random random, int count) {
    int sum = 0;
    for (int i = 0; i < count; i++) {
        sum += generateRandomNumber(random);
    }

    return sum;
}

public static int generateRandomNumber(Random random) {
    return random.nextInt(6) + 1; //return number between 1 to 6
}

}

解决方案:

为了模拟12个骰子的每一次可能的滚动,我使用了12个嵌套的for-loops,这样我就可以产生每一次可能的滚动。

我用这个循环代替了你随机掷骰子的过程。

int[] dice = new int[12];

for (dice[0] = 1; dice[0] <= 6; dice[0]++) {
    System.out.println("dice[0] = " + dice[0]);

    for (dice[1] = 1; dice[1] <= 6; dice[1]++) {
        System.out.println("dice[1] = " + dice[1]);

        for (dice[2] = 1; dice[2] <= 6; dice[2]++) {
            System.out.println("dice[2] = " + dice[2]);

            for (dice[3] = 1; dice[3] <= 6; dice[3]++) {
                for (dice[4] = 1; dice[4] <= 6; dice[4]++) {
                    for (dice[5] = 1; dice[5] <= 6; dice[5]++) {
                        for (dice[6] = 1; dice[6] <= 6; dice[6]++) {
                            for (dice[7] = 1; dice[7] <= 6; dice[7]++) {
                                for (dice[8] = 1; dice[8] <= 6; dice[8]++) {
                                    for (dice[9] = 1; dice[9] <= 6; dice[9]++) {
                                        for (dice[10] = 1; dice[10] <= 6; dice[10]++) {
                                            for (dice[11] = 1; dice[11] <= 6; dice[11]++) {

                                                playerOneSum = dice[0] + dice[1] + dice[2] + dice[3] + dice[4] + dice[5] + dice[6];
                                                playerTwoSum = dice[7] + dice[8] + dice[9] + dice[10] + dice[11];


                                                //find who won
                                                if (playerOneSum == playerTwoSum) {
                                                    ties++;
                                                } else if (playerOneSum > playerTwoSum) {
                                                    playerOneWins++;
                                                } else {
                                                    playerTwoWins++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

花了三四分钟才完成。为了不让我太急躁,我把println的。结果是。

Player 1 win: 1877280394
Player 2 win: 225654001
Ties: 73847941

给TA打赏
共{{data.count}}人
人已打赏
未分类

在arduino IDE中使用修改后的库

2022-9-8 3:40:38

未分类

我可以收听尤里卡的任何活动吗?

2022-9-8 3:40:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索