[英]Solving an equation using incremental nested for-loop
I am trying to solve an equation using nested forloops. 我正在尝试使用嵌套的forloops解决方程式。
I am trying to solve for (i,j,k) in the equation (-25)i + (14)j + (-8)k = -77. 我正在尝试求解方程(-25)i +(14)j +(-8)k = -77的(i,j,k)。 Norm is -77. 规范是-77。
I have been trying to debug it but cant find the problem with my algorithm. 我一直在尝试调试它,但是找不到与我的算法有关的问题。
Here is my code: 这是我的代码:
int newx = -20; int newy = -20; int newz = -20;
test = true;
while(test){
for(int i = 0; i < 20; i++){
if((f[0]*(newx+i) + f[1]*newy + f[2]*newz) == norm){
System.out.println(f[0]*(newx+i) + f[1]*newy + f[2]*newz);
} else {
for(int j = 0; j < 20; j++){
if((f[0]*(newx) + f[1]*(newy+j) + f[2]*newz) == norm){
System.out.println((f[0]*(newx) + f[1]*newy+j + f[2]*newz));
} else {
for(int k = 0; k < 20; k++){
if((f[0]*(newx) + f[1]*(newy) + f[2]*(newz+k)) == norm){
System.out.println((f[0]*(newx) + f[1]*newy + f[2]*newz+k));
}
}
}
}
}
}
}
Your approach in general is convoluted. 您的方法通常很复杂。 You're attempting to obtain the value of i
, j
and k
. 您正在尝试获取i
, j
和k
的值。 So keep it simple! 因此,保持简单!
First, you're going to need to test every possible combination, so a triple nested for loop makes sense. 首先,您将需要测试每种可能的组合,因此三重嵌套的for循环才有意义。 Then, you're going to need to run the calculation and test the output. 然后,您将需要运行计算并测试输出。 If the output is -77
, you've found one solution for i
, j
and k
. 如果输出为-77
,则为i
, j
和k
找到了一个解决方案。 So breaking this down into stages we've got. 因此,将其分解为几个阶段。
i
, j
and k
. 测试i
, j
和k
每个值。 i
, j
and k
into the formula. 将i
, j
和k
插入公式。 output
of the formula is -77
, print the results. 如果公式的output
为-77
,则打印结果。 So now we know the stages , let's turn that into some code. 因此,现在我们知道了各个阶段 ,让我们将其转换为一些代码。
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
for(int k = 0; k < 20; k++) {
// Go through each value.
int output = (-25 * i) + (14 * j) + (-8 * k);
// Plug the values into the formula.
if(output == -77) {
// Test and output.
System.out.println("i = " + i + ", j = " + j + ", k = " + k);
}
}
}
}
Now, you do not know how many solutions you are going to get here, so I would create another class called Solution
. 现在,您不知道要到达这里有多少个解决方案,因此我将创建另一个名为Solution
类。
public class Solution {
private int i;
private int j;
private int k;
// Appropriate constuctor and getters.
}
That way, when you need to save it, you can say. 这样,当您需要保存它时,您可以说。
List<Solution> mySolutions = new ArrayList<Solution>();
// Nested for loops.
mySolutions.add(new Solution(i, j, k)); // Provided that you wrote the constructor
And you can override the toString
method in the Solution
object to print out a reasonable output.. 而且,您可以覆盖Solution
对象中的toString
方法以打印出合理的输出。
public String toString() {
return "i =" + i + ", j =" + j + ", k =" + k;
}
So when you output, it will look something like.. 因此,当您输出时,它将看起来像..
i = 1, j = 2, k = 10
i = 1, j = 6, k = 17
i = 3, j = 1, k = 2
Check out.. 查看..
You are evaluating the inner if
conditions incorrectly, eg: 您正在评估内if
不正确地条件,例如:
for(int j = 0; j < 20; j++){
if((f[0]*(newx) + f[1]*(newy+j) + f[2]*newz) == norm){
System.out.println((f[0]*(newx) + f[1]*newy+j + f[2]*newz));
}
[...]
}
you forgot to add i
to newX
, it should be f[0] * (newx + i)
. 您忘记将i
添加到newX
,它应该是f[0] * (newx + i)
。 This applies to the if
condition, as well as the println(...)
. 这适用于if
条件以及println(...)
。 If you correct those bugs, you find a solution. 如果您纠正了这些错误,则可以找到解决方案。
To get rid of the endless-loop: 要摆脱无限循环:
while
loop 删除while
循环 && test
to each for
loop condition 添加&& test
到每个for
循环条件 test = false;
添加test = false;
in each if
-body 在每个if
-body中 (This solution is quick and dirty, but I will let you figure out, how to solve it in a more appropiate fashion). (此解决方案既快速又肮脏,但是我会让您想出如何以更合适的方式解决它)。
For a more elegant, overall solution please take a look at christoper's answer . 要获得更优雅的整体解决方案,请查看christoper的答案 。 There you find a (from a software engineer's point of view) cleaner solution, as well as some extensions you might want to have. 在那里,您可以找到(从软件工程师的角度来看)更清洁的解决方案,以及您可能想要的一些扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.