java - 使用加法、减法或乘法可以使 5 个数字等于目标数字吗

标签 java math permutation

我正在开发一个游戏,很像数学骰子问题,尽管有点不同。用户掷 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 次,他们可以完成所有这三个操作,然后一次获得所有三个答案。

如果你想以不同的方式查看它,这里是粘贴箱: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();
    }
}
}

最佳答案

我看到你自己找到了答案,但我也尝试解决你的问题,无论如何我决定在这里发布另一个解决方案:

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
        Test test = new Test();
        test.combineOperators();
        Scanner scanner = new Scanner(System.in);

        int result = scanner.nextInt();       //get input
        int[] numbers = new int[5];
        for(int i = 0; i <5; i++){
            numbers[i] = scanner.nextInt();
        }

        ArrayList<Integer> results = test.operationsOnArrays(numbers, test.combineOperators());         //check for results
        if(results.contains(result)){
            System.out.println(result + " is a possible solution");
        }else{
            System.out.println(result + " is not a possible solution");
        }

    }
    public ArrayList<String[]> combineOperators(){             //create all possible combinations of operators
        String[] signs = {"+","-","*"};
        ArrayList<String[]> combinations = new ArrayList<String[]>();
        for(String a : signs){
            for (String b : signs){
                for(String c : signs){
                    for(String d: signs){
                            String[]temp = {a,b,c,d};
                            combinations.add(temp);
                    }
                }
            }
        }
        return  combinations;
    }

    public ArrayList operationsOnArrays(int[] num, ArrayList<String[]> combinations){   //do the math with every combination on given ints
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(String[] operators : combinations){         //for every operators combination
            int result = num[0];
            for(int i = 0; i<=3 ; i++){
                result = doTheMath(operators[i],result,num[i+1]);      // take two ints and operator
            }
            list.add(result);
        }
        return list;
    }

    public int doTheMath(String operator, int prev, int next){   // it take two ints from input array, and do operation
        if(operator.equals("+")){                                // determined by a taken operator
            return prev + next;
        }else  if(operator.equals("-")){
            return prev - next;
        }else if(operator.equals("*")){
            return prev  *next;
        }
        return 0;
    }
}

我认为这样,扩展起来很简单,可以扩展更多的数字或运算符,甚至可以实现输入数字的重新排序。

关于java - 使用加法、减法或乘法可以使 5 个数字等于目标数字吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008414/

相关文章:

javascript - 如何找到n位2位数字的所有排列

math - float 学坏了吗?

c - 32 副牌的具体排列(C 语言)

java - 在使用 Map.Entry<String, Map<Date, List<SomeObject>>> 的三个 for-each 叠瓦式中,当我迭代时为什么在第二个 for-each 循环中出现异常?

java - 如何通过自己的类删除一个对象?

algorithm - 用于减少电话号码位数的压缩扩展算法

数学问题 : number of different permutations

python - 生成列表中每个单词的所有组合和排列

java - 从 java 类中查找非 java 文件

java - 破坏性地删除链表中的所有其他元素