使用嵌套的For循环的蛮力方程求解器

时间:2020-10-04 15:38:14

标签: java

在本实验中,您将使用蛮力算法编写自己的方法来求解二次多项式。 此方法将包含四个参数,三个系数和一个常数,并且将具有以下签名:

public static String bruteForceEquationSolver(int one, int two, int three, int constant){}

根据上述参数,您要求解的方程式具有以下格式:

(constant) = (one) * x + (two) * y + (three) * z XYZ是您要寻找解决方案的变量。

例如,如果要使用参数(2, 3, 4, 42)调用该方法,则会找到方程2x + 3y + 4z = 42的解决方案。

完成此方法后,请从main调用testBFES()方法,以确保其按预期工作。

该方法的一些规范:

您的方法应该为每个变量尝试(1-10)范围内的所有可能性。使用嵌套的for循环检查可能性,从x开始,然后是y,然后是z。 如果找到解决方案,您的方法应该返回,并以以下格式返回包含解决方案的字符串:

x:xSolution y:ySolution z:zSolution

在解决方案和下一个变量之间应该有一个空格,每个变量字母后面应有一个冒号和一个空格。

如果找不到解决方案,则返回字符串“找不到解决方案。”。

这是我到目前为止所拥有的,我不知道我是否设置正确。

public static String bruteForceEquationSolver(int one, int two, int three, int constant) {
    // student code here
    do {
        for (int i = 1; i <= 10; i++) {
            one *= i;
            for (int j = 1; j <= 10; j++) {
                two *= j;
                for (int k = 1; k <= 10; k++) {
                    three *= k;
                }
            }

        }
    } while(one + two + three <= constant);

        return String.format("x:%d y:%d z:%d", one, two, three); 
}


 public static void testBFES() {
     System.out.println("Testing Brute Force Equation Solver");
     String expected = "x: 2 y: 3 z: 4";
     System.out.println("Expecting: " + expected);

     String actual = bruteForceEquationSolver(3, 4, 6, 42);
     System.out.println("Actual: " + actual);

     boolean correct = expected.equals(actual);
     System.out.println("Outputs equal? " + correct);
     }

     public static void testMT() {
     System.out.println("Testing Multiplication Table");

     String expected = "1\t2\t3\t4\t\n2\t4\t6\t8\t\n3\t6\t9\t12\t\n";
     System.out.print("Expecting:\n" + expected);

     String actual = multiplicationTable(3, 4);
     System.out.print("Actual:\n" + actual);

     boolean correct = expected.equals(actual);
     System.out.println("Outputs equal? " + correct);
     }

2 个答案:

答案 0 :(得分:0)

使用g_bor提出的更正仅能解决1到10之间的整数的问题。您可以逐渐增加整数的大小,例如通过增大边界来限制|x|+|y|+|z|。我的想法是如何做到的:

for(int bound = 0; bound >= 0; bound++){//stopping when overflowing
    for(int x = -bound; x <= bound; x++){//other x violates bound
        for(int y = -bound + Math.abs(x); y <= bound - Math.abs(x); y++){//force that |x|+|y| <= bound
            // Only test |x| + |y| + |z| = bound cases as smaller ones were already tested in previous iterations
            for(int z_sign = -1; z_sign <= 1; z_sign += 2){
                int z = bound - Math.abs(x) - Math.abs(y);//|x|+|y|+|z|=bound
                z *= z_sign;
                if(one*x + two*y + three*z == constant){
                    //TODO format and return solution (x,y,z)
                }
            }
        }
    }
}
//TODO other return or exception if no solution found

请注意,这只会找到带有|x|+|y|+|z| <= MAXINT的所有整数解。因此,可能希望将int变量替换为long的{​​{1}}。

答案 1 :(得分:0)

蛮力方程求解器

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    // equation format x1 + y1 = c1
    // input will be   8 7 38
    // x1 = 8, y1 = 7, c1 = 38
    System.out.println("Enter first Equation:");
    String input1 = scanner.nextLine();
    String[] split = input1.split(" ");
    int x1 = Integer.parseInt(split[0]);
    int y1 = Integer.parseInt(split[1]);
    int c1 = Integer.parseInt(split[2]);
    System.out.println("Enter Second Equation:");
    String input2 = scanner.nextLine();
    split = input2.split(" ");
    int x2 = Integer.parseInt(split[0]);
    int y2 = Integer.parseInt(split[1]);
    int c2 = Integer.parseInt(split[2]);
    for (int x = -10; x <= 10; x++) {
        for (int y = -10; y <= 10; y++) {
            if (((x1 * x) + (y1 * y)) == c1 && ((x2 * x) + (y2 * y)) == c2) {
                System.out.println(String.format("x = %s, y = %s", x, y));
                return;
            }
        }
    }
    System.out.println("There is no solution");