【发布时间】:2015-05-02 22:55:36
【问题描述】:
我正在开发一款游戏,很像数学骰子问题,虽然有点不同。用户掷一个 20 面骰子,然后再掷 5 个骰子。为了简单起见,用户不能重新排列骰子,所以如果他们掷出1 2 3 4 5,他们就不能进行1 + 3 + 2 + 5 + 4 之类的操作。问题是,如果使用加法、减法和乘法,它们是否可以从 20 面骰子中达到目标数量?
现在,我知道如何做到这一点,只需生成 5 个数字的所有可能的加法、减法和乘法的排列,但它是 实现得到我的解决方案。几次尝试后我遇到了障碍,因此感谢您提供任何帮助。
编辑:这是我当前的实现,没有乘法,它工作得不太正确。
import java.util.ArrayList;
import java.util.Scanner;
public class targetDice {
public static void main(String[] args) {
ArrayList<Integer> rolls = new ArrayList<Integer>(); // Array to hold the rolls
ArrayList<Integer> d20 = new ArrayList<Integer>(); // Array to hold all the d20 rolls
Scanner sc = new Scanner(System.in);
int answer = 0;
String record = "";
while (sc.hasNextInt()) {
d20.add(sc.nextInt()); // Adds the d20 rolls
rolls.add(sc.nextInt()); // Adds the first roll
rolls.add(sc.nextInt()); // Adds the second roll
rolls.add(sc.nextInt()); // Adds the third roll
rolls.add(sc.nextInt()); // Adds the fourth roll
rolls.add(sc.nextInt()); // Adds the fifth roll
} // End while loop
for (int i = 0; i < d20.size(); i++) { // Number of times we need to compute: number of d20 rolls
answer = rolls.get(0);
for (int j = 0; j < rolls.subList(0, 5).size(); j++) { // Go through each roll given
if (d20.get(i) > answer || d20.get(i).equals(answer)) { // If the d20 roll is higher than the first roll or if it's equal
answer += rolls.get(j);// then take the running total and add it
record += " + ";
} else if (d20.get(i) < answer) {
answer -= rolls.get(j);
record += " - ";
}
}
System.out.println(answer);
//TODO: This if else block is our final product. It should be fine.
if (answer == d20.get(i)) // If the combo is equal the d20 roll
System.out.println("Solution"); // Print solution
else
System.out.println("No Solution"); // Otherwise print no solution
rolls.subList(0, 5).clear(); // Clears out the first 5 elements to make coding easier
answer = 0; // Reset the answer var
System.out.println(record);
} // End For loop
} // End main
} // End class
它的设置是为了让用户可以多次掷骰子,如果他们要尝试这个游戏 3 次,他们可以同时做三个然后得到所有三个答案。
如果您想以不同的方式查看它,这里是 pastebin:http://pastebin.com/PRB0NKpN
编辑 2:这是我的最终解决方案。有点粗鲁。
import java.util.ArrayList;
import java.util.Scanner;
public class testClass {
public static void main(String[] args) {
ArrayList<Integer> d20 = new ArrayList<Integer>();
ArrayList<Integer> rolls = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
d20.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
}
int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
for (int x = 0; x < d20.size(); x++) {
int wright = 0, rong = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
for (int k = 1; k < 4; k++) {
for (int m = 1; m < 4; m++) {
if (i == 1) {
num1 = rolls.get(0) + rolls.get(1);
} else if (i == 2) {
num1 = rolls.get(0) - rolls.get(1);
} else if (i == 3) {
num1 = rolls.get(0) * rolls.get(1);
}
if (j == 1) {
num2 = num1 + rolls.get(2);
} else if (j == 2) {
num2 = num1 - rolls.get(2);
} else if (j == 3) {
num2 = num1 - rolls.get(2);
}
if (k == 1) {
num3 = num2 + rolls.get(3);
} else if (k == 2) {
num3 = num2 - rolls.get(3);
} else if (k == 3) {
num3 = num2 * rolls.get(3);
}
if (m == 1) {
num4 = num3 + rolls.get(4);
} else if (m == 2) {
num4 = num3 - rolls.get(4);
} else if (m == 3) {
num4 = num3 * rolls.get(4);
}
if (d20.get(x) == num4) {
wright = 1;
}
}
}
}
}
if (wright == 1)
System.out.println("Case " + (x+1) + ": Solution");
else
System.out.println("Case " + (x+1) + ": No Solution");
rolls.subList(0, 5).clear();
}
}
}
【问题讨论】:
-
添加您尝试过的代码并指出错误。
-
要保持操作顺序吗? IE。
1 + 2 * 3应该是9(忽略操作顺序)还是7(尊重操作顺序)?后者使您的问题变得更加复杂。 -
@Turing85 啊,我的错,不它确实不需要遵循操作的顺序。
-
@JorgeCampos 就是这样,我不确定在哪里我搞砸了。
-
“它工作不正常” 不是一个足够具体的问题陈述。将代码添加到问题中会很有帮助,但仅存在代码并不能确保问题足够缩小以成为此处的主题。您需要弄清楚您的算法中具体的哪些内容没有按您的预期工作,并在您的问题中明确说明该信息。
标签: java math permutation