使用一个 6 面骰子,计算达到某个数字的可能方式
With a single 6-faced dice, calculate the possible ways to reach a certain number
我有以下 Java 作业。在棋盘游戏中,我可以掷一个 6 面骰子并向前移动相同数量的空格。如果目标距起点“n”格,我如何实现一个程序来计算所有可能的方式以准确到达 "n"?我正在绞尽脑汁,但找不到解决方案,我已经搜索了网络,但没有什么是我想要的。我知道这并不难,我只是找不到正确的方法。
提前致谢。
有多种方法可以做到这一点,所以我将保持通用。
确定所有可能的排列,存储值。产生一卷,得到总数。筛选与总数匹配的排列。
class Roll {
int first;
int second;
int total;
public Roll(int first, int second) {
this.first = first;
this.second = second;
this.total = first + second;
}
List<Roll> permutations = new ArrayList<>();
for (int ii = 1; ii < 7; ii++)
for (int jj = 1; jj < 7; jj++)
permutations.add(new Roll(ii,jj);
roll the dice, determine the total;
now loop through the permutations to find the
total matching the roll total. These are your combinations.
This solution doesn't handle duplicate rolls. For example 1,6 and 6,1;
我有以下 Java 作业。在棋盘游戏中,我可以掷一个 6 面骰子并向前移动相同数量的空格。如果目标距起点“n”格,我如何实现一个程序来计算所有可能的方式以准确到达 "n"?我正在绞尽脑汁,但找不到解决方案,我已经搜索了网络,但没有什么是我想要的。我知道这并不难,我只是找不到正确的方法。
提前致谢。
有多种方法可以做到这一点,所以我将保持通用。 确定所有可能的排列,存储值。产生一卷,得到总数。筛选与总数匹配的排列。
class Roll {
int first;
int second;
int total;
public Roll(int first, int second) {
this.first = first;
this.second = second;
this.total = first + second;
}
List<Roll> permutations = new ArrayList<>();
for (int ii = 1; ii < 7; ii++)
for (int jj = 1; jj < 7; jj++)
permutations.add(new Roll(ii,jj);
roll the dice, determine the total;
now loop through the permutations to find the
total matching the roll total. These are your combinations.
This solution doesn't handle duplicate rolls. For example 1,6 and 6,1;